Answer the question
In order to leave comments, you need to log in
How to compare 2 json files?
Hello everyone, I have 2 JSON files that come from different APIs:
the 1st file looks like this:
{
"data": [
{
"id": 1025
},
{
"id": 2000
},
{
"id": 2500
}
]
}
{
"data": [
{
"IDNum": 1025,
"count": 1
},
{
"IDNum": 2000,
"count": 3
},
{
"IDNum": 2500,
"count": 2
}
]
}
Answer the question
In order to leave comments, you need to log in
First prepare the second JSON
var arrmap = {}
for (x in arr['data']) {
var v = arr['data'][x]
arrmap[v['IDNum']] = v['count']
}
for (x in foo['data']) {
var v = foo['data'][x]
if (arrmap[v['id']]) {
console.log(arrmap[v['id']])
}
}
An option to use find to find an entry:
var file2 = data2
var obj = getElement(file2, 1025)
if (obj) console.log(obj.count)
function getElement(file, id) {
return file['data'].find(el => el.IDNum === id)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question