> For the complete documentation index, see [llms.txt](https://donedeal0.gitbook.io/superdiff/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://donedeal0.gitbook.io/superdiff/features/getobjectdiff.md).

# getObjectDiff

## IMPORT

```typescript
import { getObjectDiff } from "@donedeal0/superdiff";
```

***

## FORMAT

### Input

```typescript
prevData: Record<string, unknown>;
nextData: Record<string, unknown>;
options?: {
  ignoreArrayOrder?: boolean, // false by default,
  showOnly?: {
    statuses: ("added" | "deleted" | "updated" | "equal")[], // [] by default
    granularity?: "basic" | "deep" // "basic" by default
  }
}
```

* `prevData`: the original object.
* `nextData`: the new object.
* `options`
  * `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
  * `showOnly`: returns only the values whose status you are interested in. It takes two parameters:
    * `statuses`: status you want to see in the output (e.g. `["added", "equal"]`)
      * `granularity`:
        * `basic` returns only the main properties whose status matches your query.
        * `deep` can return main properties if some of their subproperties' status match your request. The subproperties are filtered accordingly.

### Output

```typescript
type ObjectDiff = {
  type: "object";
  status: "added" | "deleted" | "equal" | "updated";
  diff: Diff[];
};

type Diff = {
  key: string;
  value: unknown;
  previousValue: unknown;
  status: "added" | "deleted" | "equal" | "updated";
  // recursive diff in case of subproperties
  diff?: Diff[];
};
```

***

## USAGE

### Input

```diff
getObjectDiff(
  {
    id: 54,
    user: {
      name: "joe",
-     member: true,
-     hobbies: ["golf", "football"],
      age: 66,
    },
  },
  {
    id: 54,
    user: {
      name: "joe",
+     member: false,
+     hobbies: ["golf", "chess"],
      age: 66,
    },
  }
);
```

### Output

```diff
{
      type: "object",
+     status: "updated",
      diff: [
        {
          key: "id",
          value: 54,
          previousValue: 54,
          status: "equal",
        },
        {
          key: "user",
          value: {
            name: "joe",
            member: false,
            hobbies: ["golf", "chess"],
            age: 66,
          },
          previousValue: {
            name: "joe",
            member: true,
            hobbies: ["golf", "football"],
            age: 66,
          },
+         status: "updated",
          diff: [
            {
              key: "name",
              value: "joe",
              previousValue: "joe",
              status: "equal",
            },
+           {
+             key: "member",
+             value: false,
+             previousValue: true,
+             status: "updated",
+           },
+           {
+             key: "hobbies",
+             value: ["golf", "chess"],
+             previousValue: ["golf", "football"],
+             status: "updated",
+           },
            {
              key: "age",
              value: 66,
              previousValue: 66,
              status: "equal",
            },
          ],
        },
      ],
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://donedeal0.gitbook.io/superdiff/features/getobjectdiff.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
