2

I have this JSON data:

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"
 }]

And 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"
}]

And with this for and nested for I must filter data from arr and push it to results[] if is match listId and permission in 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 problems is I get listId undefined if arr.length is smaller than lists.length

Any idea how can I solve this?

micstr
5,2568 gold badges52 silver badges80 bronze badges
asked Sep 22, 2017 at 6:43

2 Answers 2

5

The problems is I get listId undefined if arr.length is smaller then lists.length

You need to use the respective indexes for their own arrays, i.e. i for arr and j for lists

if( (arr[i].listId == lists[j].listId) && (arr[i].permission == lists[j].permission) )
answered Sep 22, 2017 at 6:45
Sign up to request clarification or add additional context in comments.

Comments

3

You could change the structure a bit with a look up table for permissions and use only a single loop for the result set.

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" }],
 array = [{ 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" }],
 permissions = {},
 result;
lists.forEach(function (p) {
 permissions[p.listId] = permissions[p.listId] || {};
 permissions[p.listId][p.permission] = true;
});
result = array.filter(function (a) {
 return permissions[a.listId] && permissions[a.listId][a.permission];
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Sep 22, 2017 at 6:53

1 Comment

Yes, nice example!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.