Having the JSON data below:
var lists = [{
"listId": 1,
"permission": "WRITE"
}, {
"listId": 2,
"permission": "WRITE"
}, {
"listId": 2,
"permission": "READ"
}, {
"listId": 3,
"permission": "READ"
}, {
"listId": 3,
"permission": "WRITE"
}, {
"listId": 5,
"permission": "WRITE"
}]
As well as this one:
var arr = [{
"listId": 1,
"confidentiality": "PUBLIC",
"listName": "List name here..1",
"permission": "WRITE"
}, {
"listId": 2,
"confidentiality": "PUBLIC",
"listName": "List name here..2",
"permission": "READ"
}, {
"listId": 3,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..3",
"permission": "WRITE"
}, {
"listId": 4,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..4",
"permission": "WRITE"
}, {
"listId": 5,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..5",
"permission": "WRITE"
}]
The task at hand is to filter data from arr and push it to results[] if there is a match in listId and permission within lists[...].
var result = [];
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < lists.length; j++) {
if( (arr[j].listId == lists[i].listId) && (arr[j].permission == lists[i].permission) ) {
result.push(arr[j]);
}
}
}
console.log(result);
The issue arises when I encounter listId undefined if arr.length is smaller than lists.length.
Any suggestions on how to tackle this problem?