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
How to Sort Data in Ascending or Descending Order in Reactjs
Remove Everything After Last Backslash
Expected Linebreaks to Be 'Lf' But Found 'Crlf' Linebreak-Style
Allow Only One Checked Checkbox, Angular Best Practice.
React Native: Image Not Displaying With Dynamic/Local Uri
How to Make a Owl Carousel With Arrows Instead of Next Previous
How to Save an Image to Localstorage and Display It on the Next Page
How to Make a Auto-Increment Id in My Table Td Using Jquery
Run Script After Appending It to the HTML
How to Synchronize the Scroll Position of Two Divs
Delete Span Tag Along With Text in HTML Permanently
Property Does Not Exist on Type 'Never' Typescript/React
How to Access the Value of a Promise
Pass Node Js Variable to Ejs Template
How to Assign [(Ngmodel)] With an Empty/Null/Undefined Object - Angular 4
How to Look for Changes in Localstorage in Angular 4
How to Remove HTML Element from Dom With React
Bootstrap Button Drop-Down Inside Responsive Table Not Visible Because of Scroll