John Glabb
John Glabb

Reputation: 1621

How to compare two objects with lodash and return new object?

Here are two objects I need to compare and return specific values from object1 if found in object2.

object1 = {
  "body": {
    "items": [
      {
        "data": {
          "name": "Smith",
          "status": "Ready",
          "userinfo": [
            {
              "dob": "01/01/2000",
              "nickname": "Joe"
            }
          ]
        },
        "workinfo": {
          "company": "mycompany",
          "address": "101 Main str."
        }
      },
      {
        "data": {
          "name": "Luke",
          "status": "Ready",
          "userinfo": [
            {
              "dob": "01/01/2001",
              "nickname": "LL"
            }
          ]
        },
        "workinfo": {
          "company": "mycompany",
          "address": "101 Main str."
        }
      }
    ]
  }
}

Object2 is even simple one:

object2 = {
  "items": [
    {
      "name": "Smith",
      "status": "Ready"
    },
    {
      "name": "Luke",
      "status": "Ready"
    }
  ]
}

So if Object1 body.items[x].data.name found in Object2 items.name then finally I need to get new object like this:

object3 = {{name: "Smith", status: "Ready"}, {name: "Luke", status: "Ready"}}

Upvotes: 1

Views: 58

Answers (3)

Akrion
Akrion

Reputation: 18515

With ES6 this is an easy task with Array.filter and Array.some:

const obj1 = {"body":{"items":[{"data":{"name":"Smith","status":"Ready","userinfo":[{"dob":"01/01/2000","nickname":"Joe"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}},{"data":{"name":"Luke","status":"Ready","userinfo":[{"dob":"01/01/2001","nickname":"LL"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}}]}}
const obj2 = {"items":[{"name":"Smith","status":"Ready"},{"name":"Luke","status":"Ready"}]}

const r = obj2.items.filter(x => obj1.body.items.some(y => y.data.name == x.name))

console.log(r)

Since you have the objects in the correct layout in obj2 you can start from there and filter them against obj1.

Upvotes: 0

Ori Drori
Ori Drori

Reputation: 191976

You can use _.intersectionWith() to return items from obj2, that their name equals data.name in obj2 items:

const obj1 = {"body":{"items":[{"data":{"name":"Smith","status":"Ready","userinfo":[{"dob":"01/01/2000","nickname":"Joe"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}},{"data":{"name":"Luke","status":"Ready","userinfo":[{"dob":"01/01/2001","nickname":"LL"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}}]}}
const obj2 = {"items":[{"name":"Smith","status":"Ready"},{"name":"Luke","status":"Ready"}]}

const result = _.intersectionWith(obj2.items, obj1.body.items, 
  (a, b) => _.get(a, 'name') === _.get(b, 'data.name')
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Upvotes: 0

Kobe
Kobe

Reputation: 6446

You can use filter and find:

var obj1 = {
  "body": {
    "items": [{
        "data": {
          "name": "Smith",
          "status": "Ready",
          "userinfo": [{
            "dob": "01/01/2000",
            "nickname": "Joe"
          }]
        },
        "workinfo": {
          "company": "mycompany",
          "address": "101 Main str."
        }
      },
      {
        "data": {
          "name": "Luke",
          "status": "Ready",
          "userinfo": [{
            "dob": "01/01/2001",
            "nickname": "LL"
          }]
        },
        "workinfo": {
          "company": "mycompany",
          "address": "101 Main str."
        }
      }
    ]
  }
}

var obj2 = {
  "items": [{
      "name": "Smith",
      "status": "Ready"
    },
    {
      "name": "Luke",
      "status": "Ready"
    }
  ]
}

var output = obj2.items.filter(({name}) => obj1.body.items.find(({data}) => name === data.name))

console.log(output)

Filter will return all the objects that pass the find condition, that is, if the name is found in obj1

Upvotes: 1

Related Questions