1

I have an array with objects A:

A = [ 
     { 
       id: 12345,
       folder: 'folder1',
       name: 'Name1'
     },
     { 
       id: 12346,
       folder: 'folder1',
       name: 'Name2'
     },
     { 
       id: 12347,
       folder: 'folder1',
       name: 'Name3'
     },
     { 
       id: 12348,
       folder: 'folder1',
       name: 'Name4'
     }
    ]

and an array B with ids:

B = [12345, 12348]

I would like to filter/get a new array with "folder" + "name" from A based on the ids from B

res = ["folder1/Name1", "folder1/Name4"]

Not sure how to "filter" A based on ids in B?

2

6 Answers 6

3

A very quick way to do this would be to use filter and map.

A.filter(function(a) {
   return B.indexOf(a.id) >= 0;
})
.map(function(a) {
   return a.folder+'/'+a.name;
});
Sign up to request clarification or add additional context in comments.

Comments

2

Just another approach; use the reduce method;

var result = A.reduce(function(acc, current) {
    if(B.indexOf(current.id) !== -1) {
        acc.push(current.folder + '/' + current.id);
    }

    return acc;
}, []);

DEMO: http://jsbin.com/biqicahaje/1/edit?js,console

The advantage over @Clint Powell method (which is also pretty good, and more readable/maintainable) is that you will loop only through A.length elements, instead of A.length + B.length what depending on the size of both arrays can on cannot be an optimization.

Comments

2
function findBy(array, search) {
  var res = [],
      arrayLen  = array.length,
      i;

  for (i = 0; i < arrayLen; i++) {
    if (~search.indexOf(array[i].id)) {
      res.push(array[i].folder + '/' + array[i].name)  
    }    
  }

  return res;
}

Demo: http://jsbin.com/carem/2/edit

Comments

1

You could create a loop within a loop to do this. Check each element of the first array then if the id's match push the data folder/name to a new array...

Here is an example:

<script type="text/javascript">
A = [ 
     { 
       id: 12345,
       folder: 'folder1',
       name: 'Name1'
     },
     { 
       id: 12346,
       folder: 'folder1',
       name: 'Name2'
     },
     { 
       id: 12347,
       folder: 'folder1',
       name: 'Name3'
     },
     { 
       id: 12348,
       folder: 'folder1',
       name: 'Name4'
     }
    ];

B = [12345, 12348];

***********************************************New Code**********************************************

var result = [];

for(var i = 0; i < A.length; i++)
{
    for(var j = 0; j < B.length; j++)
    {
        if(A[i].id == B[j])
        {
           result.push(A[i].folder + "/" + A[i].name); 
        }
    }
}
for(var i = 0; i < result.length; i++)
{
   window.alert(result[i]);
}
</script>

Comments

0

Pretty much as brso05 said.

Just loop over B using a simple for loop similar to this snippet:

for (var i = 0; i < B.length; i++) {
    var id = B[i];
}

For each iteration on B, run through A and put the results into a results array:

for (var j = 0; j < A.length; j++) {
    if (A[j].id == id) {
        results.push(A[j].folder + '/' + A[j].name);
    }
}

Combining this, the following snippet should suffice to your needs:

var results = new Array();

for (var i = 0; i < B.length; i++) {
    var id = B[i];

    for (var j = 0; j < A.length; j++) {
        if (A[j].id == id) {
            results.push(A[j].folder + '/' + A[j].name);
        }
    }
}

Comments

0
var A = [ 
 { 
   id: 12345,
   folder: 'folder1',
   name: 'Name1'
 },
 { 
   id: 12346,
   folder: 'folder1',
   name: 'Name2'
 },
 { 
   id: 12347,
   folder: 'folder1',
   name: 'Name3'
 },
 { 
   id: 12348,
   folder: 'folder1',
   name: 'Name4'
 }
];
var B = [12345, 12348];
var result = [];
for(i in B){
    var this_id = B[i];
    for(j in A){
        if(A[j].id == this_id){
            result.push(A[j].folder+"/"+A[j].name);
        }
    }
}
console.log(result);

Comments

Your Answer

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.