I'm writing a code that need to compare 2 json arrays and create a new array. data is the main array that is used to compare, nData is 2nd json array where we are comparing the values. here is the criteria.
- If
nData's Id is equal todata'sknowledge__c, create a new key(namedicon) and value asbin - Else create a new key(named
icon) and value asnon bin.
Here is my code
var data = [{
"Id": "a8109000000CcktAAC",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000oq3ZAAQ",
"ResearchState__c": "Not verified"
}, {
"Id": "a8109000000Ccl4AAC",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000oiqdAAA",
"ResearchState__c": "Not verified"
}, {
"Id": "a8109000000Ccm1AAC",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000orsBAAQ",
"ResearchState__c": "In verification"
}, {
"Id": "a8109000000CcNFAA0",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000oipoAAA",
"ResearchState__c": "Confirmed Partial Solution"
}, {
"Id": "a8109000000CcNZAA0",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000onvHAAQ",
"ResearchState__c": "Not verified"
}, {
"Id": "a8109000000CcNPAA0",
"DiagBinder__c": "a8009000000boTeAAI",
"Knowledge__c": "ka109000000olFxAAI",
"ResearchState__c": "In verification"
}];
var nData = [{
"Id": "ka109000000ors1AAA"
}, {
"Id": "ka109000000oq3ZAAQ"
}, {
"Id": "ka109000000oiqdAAA"
}, {
"Id": "ka109000000oiqDAAQ"
}, {
"Id": "ka109000000oir9AAA"
}, {
"Id": "ka109000000oiqCAAQ"
}];
var articleList = [];
let myArticle = {};
for (let article of nData) {
myArticle = article;
var newA = data.filter((item) => {
if (item.Knowledge__c == article.Id)
myArticle.icon = 'Non bin';
else
myArticle.icon = 'bin';
});
articleList.push(myArticle);
}
console.log(articleList);
In my current code its adding bin to all the objects. i.e. else part is getting triggered.
Please let me know where am I going wrong and how to fix it.
Thanks