0

I have 2 array as below.

I want to keep "id" of array 2 not exist in array 1.

Array 1

{"list":{"0":{"category":"Cat fr 1","list":{"0":{"id":123123123123},"1":{"id":123123123123}

Array 2

{"list":{"0":{"category":"Cat fr 1","list":{"0":{"id":123123123123},"1":{"id":123123123123},"1":{"category":"Cat fr 2","list":{"0":{"id":123123123123},"1":{"id":123123123123},"2":{"category":"Cat fr 3","list":{"0":{"id":123123123123},"1":{"id":123123123123}

Result

{"list":{"1":{"category":"Cat fr 2","list":{"0":{"id":123123123123},"1":{"id":123123123123},"2":{"category":"Cat fr 3","list":{"0":{"id":123123123123},"1":{"id":123123123123}
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43

2 Answers2

0

You could filter() array2 into a new array (or back into array2 or array1) in combination with findIndex().

let array1 = [ {
    "id": 1,
    "name": "one"
}, {
    "id": 2,
    "name": "two"
} ];
let array2 = [ {
    "id": 1,
    "name": "one"
}, {
    "id": 2,
    "name": "two"
}, {
    "id": 3,
    "name": "three"
}, {
    "id": 4,
    "name": "four"
} ];

let newArray = array2.filter( ( item ) => {
    return array1.findIndex( ( _item ) => item.id == _item.id ) == -1;
} );
console.table( newArray );
// logs: [ { id: 3, name: "three" }, { id: 4, name: "four" } ]

I think you can apply this to your arrays.

Some more explanation:
filter() loops over all items in the array and returns a new array with objects that passes our filter condition. Therefore you need declare a new array before it so you can put it somewhere. let newArray = ...
findIndex() returns an index just like indexOf() but you can give it a condition. In this instance, it returns the index of the object whose id is the same of the id of the item from the array we are filtering. item.id == _item.id
It will return -1 when it doesn't find any such object. You want to put those object that return -1 into the new array. You want to "filter" them into the new array. That's why we check for it: ... == -1.

Paul van den Dool
  • 3,020
  • 3
  • 20
  • 44
0

var array1=jQuery.parseJSON('{"list":{"0":{"category":"Cat fr 1","list":{"0":{"id":123123123123},"1":{"id":123123123123}}}}}');

var array2=jQuery.parseJSON('{"list":{"0":{"category":"Cat fr 1","list":{"0":{"id":123123123123},"1":{"id":123123123123}}},"1":{"category":"Cat fr 2","list":{"0":{"id":123123123123},"1":{"id":123123123123}}},"2":{"category":"Cat fr 3","list":{"0":{"id":123123123123},"1":{"id":123123123123}}}}}');

var list1=array1["list"];
var list2=array2["list"];
var checkArray=[];
var result={};
for (var key in list1) {
  if (list1.hasOwnProperty(key)) {
    var val = list1[key];
    checkArray.push(val["category"]);
  }
}
for (var key in list2) {
  if (list2.hasOwnProperty(key)) {
    var val = list2[key];
    if(!checkArray.includes(val["category"]))
    {
      result[key]=val;
    }
  }
}
var finalResult={};
finalResult["list"]=result;
console.log(JSON.stringify(finalResult));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Nivethi Mathan
  • 105
  • 1
  • 8