Comparing Two Json Arrays and Get Not Matching Values

How to get get not matching objects from two json array in javascript

It is possible to do this through filter() and some() functions and logical not operator !:

var modelType = [{    'id': 3,    'name': 'eR_Beta'  },  {    'id': 12,    'name': 'eR_Studio'  },  {    'id': 6,    'name': 'eR_OFF'  },  {    'id': 9,    'name': 'eR_Schalte'  }];
var data = [{ id: 12 }, { id: 6 }]
const result = modelType.filter(f => !data.some(d => d.id == f.id));console.log(result);

Compare two JSON and create a json with the common value

You can achieve this with different combinations of filter() :

 var linkedParticipants =[
{
"id": 3,
"name": "Participant 2",
"email": "participant2@fico.com"
},
{
"id": 2,
"name": "Participant 1",
"email": "participant1@fico.com"
},
{
"id": 1,
"name": "Libin Varghese",
"email": "LibinVarghese@fico.com"
},
{
"id": 7,
"name": "Participant 5",
"email": "participant5@fico.com"
}
];
var appointmentList = [
{
"id": 32,
"participant": {
"id": 1,
"name": "Libin Varghese",
"email": "LibinVarghese@fico.com"
}
},
{
"id": 33,
"participant": {
"id": 7,
"name": "Participant 5",
"email": "participant5@fico.com"
}
}
];


var confirmedList = appointmentList.filter((a) => {
return linkedParticipants.find((p) => p.id === a.participant.id);
});

var invitedList = linkedParticipants.filter((p) => {
return !appointmentList.find((a) => p.id === a.participant.id);
});

console.log(confirmedList)
console.log(invitedList)

Compare two json objects and append unique items

You can use array#reduce to get unique value based on value and country in an object accumulator. Then get all values from these object using Object.values().

const localData = [ { value: "qwert", country: "US" }, { value: "asdfg", country: "CA" }, { value: "zxcvb", country: "GB" }],
database = [{ value: "poiuy", country: "CA" }, { value: "qwert", country: "US" }],
getUnique = (arr1, arr2) => Object.values([...arr1, ...arr2].reduce((r,o) => {
const key = `${o.value}_${o.country}`;
r[key] ??= o;
return r;
},{}));
console.log(getUnique(localData, database));

Compare two JSON objects and return true if keys and values matches using Javascript

You could get all keys and chekc the existence in both objects and their values.

If one of the objects of the array matches with the given object, you could use Array#some.

function compare(a, b) {    return [...new Set([...Object.keys(a), ...Object.keys(b)])]        .every(k => k in a && k in b && a[k] === b[k]);}
var d1 = [{ deviceid: "867874031097770", simno: "232ff33", slot: "1" },{ deviceid: "86787403100", simno: "ss343433", slot: "2" }], d2 = { deviceid: "867874031097770", simno: "232ff33", slot: "1" }, result = d1.map(compare.bind(null, d2)), isMatching = d1.some(compare.bind(null, d2));
console.log(isMatching);console.log(result);

Comparing two JSON arrays in Karate DSL

Use karate.map() for transforms: https://github.com/intuit/karate#json-transforms

* def before = [" 123.45 "]
* def fun = function(x){ return x.trim() }
* def after = karate.map(before, fun)
* match after == ['123.45']


Related Topics



Leave a reply



Submit