I am trying to compare 2 nested json objects arrays using lodash. Following example will return true even obj1
and obj2
have different number of objects inside the array.
Basically what I need to do is compare these 2 nested json objects arrays and if the arrays are identical then needs to return true
else false
note : obj1
has price element and obj2
has countInStock which should not use to compare
Can anyone help me to solve this?
import _ from "lodash"
var obj1 = [{
"colour": {
"value": "Black",
"label": "Black"
},
"size": {
"value": "M",
"label": "M"
},
"price": "70"
},
{
"colour": {
"value": "Black",
"label": "Black"
},
"size": {
"value": "M",
"label": "M"
},
"price": "70"
},
{
"colour": {
"value": "Silver",
"label": "Silver"
},
"size": {
"value": "S",
"label": "S"
},
"price": "50"
},
{
"colour": {
"value": "Black",
"label": "Black"
},
"size": {
"value": "L",
"label": "L"
},
"price": "130"
}]
var obj2 = [{
"colour": {
"value": "Silver",
"label": "Silver"
},
"size": {
"value": "S",
"label": "S"
},
"countInStock": "3"
},
{
"colour": {
"value": "Black",
"label": "Black"
},
"size": {
"value": "M",
"label": "M"
},
"countInStock": "10"
},
{
"colour": {
"value": "Black",
"label": "Black"
},
"size": {
"value": "L",
"label": "L"
},
"countInStock": "3"
}]
var result = _.isEqual(
_.omit(obj1.sort, ['countInStock']),
_.omit(obj2.sort, ['price'])
);
console.log(result); // true