0

I am trying to Remove Duplicate Objects From Javascript Array.

I have a object like this

var actualObj = [
  {
    "name": "Sam",
    "id": "1",
    "dept": "Inventory",
    "info": {
      "key1": "test1",
      "key2": "test2",
      "key3": "test3"
    }
  },
  {
    "name": "Paul",
    "id": "2",
    "dept": "Inventory",
    "info": {
      "key1": "test1",
      "key2": "test2",
      "key3": "test3"
    }
  },
  {
    "name": "Sam",
    "id": "1",
    "dept": "Inventory",
    "info": {
      "key4": "test4",
      "key5": "test5",
      "key6": "test6"
    }
  }
]

where I am trying to remove duplicates and combine the "info" into object array, something like this

var expectedObj = [
  {
    "name": "Sam",
    "id": "1",
    "dept": "Inventory",
    "info": [
      {
        "key1": "test1",
        "key2": "test2",
        "key3": "test3"
      },
      {
        "key4": "test4",
        "key5": "test5",
        "key6": "test6"
      }
    ]
  },
  {
    "name": "Paul",
    "id": "2",
    "dept": "Inventory",
    "info": {
      "key1": "test1",
      "key2": "test2",
      "key3": "test3"
    }
  }
]

With same value in "info" object, I tried with Lodash which works fine JSFIDDLE

Can anyone help me in achieving the expected object from the actual object. I am trying to create expected object by Combining as one object with similar id value.

1

2 Answers 2

1

Using lodash, _.groupBy() the items by id, then _.map() the groups into the requested format, using _.omit() to get the base object without the info, and _.map() to get an array of info. Combine to a single object using _.assign():

var actualObj = [{"name":"Sam","id":"1","dept":"Inventory","info":{"key1":"test1","key2":"test2","key3":"test3"}},{"name":"Paul","id":"2","dept":"Inventory","info":{"key1":"test1","key2":"test2","key3":"test3"}},{"name":"Sam","id":"1","dept":"Inventory","info":{"key4":"test4","key5":"test5","key6":"test6"}}];

var result = _(actualObj)
  .groupBy('id')
  .map(function(group) {
    return _.assign(_.omit(group[0], 'info'), {
      info: _.map(group, 'info')
    });
  })
  .value();
  
console.log(result);
  
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

1

you may try this, hope it will work for you.

for(let i = 0; i < actualObj.length; i++) {
    let o = actualObj[i];
    for(let j = i+1; j < actualObj.length; j++) {
        let b = actualObj[j];
        // dublicate object identified by id
        if (o.id === b.id) {
            const info = [];
            info.push(o.info);
            info.push(b.info);
            o.info = info;
            actualObj.splice(j, 1); 
        }
    }
}

If your duplicate object is/are identify by some other property like name and dept also then just update the if condition like

if (o.id === b.id && o.name === b.name && o.dept === b.dept)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.