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?

James Bund
  • 183
  • 2
  • 14
  • 1
    See http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes – Slippery Pete Nov 26 '14 at 14:20
  • possible duplicate of [How can I only keep items of an array that match a certain condition?](http://stackoverflow.com/questions/27131984/how-can-i-only-keep-items-of-an-array-that-match-a-certain-condition) – Scimonster Nov 26 '14 at 16:12

6 Answers6

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;
});
Clint Powell
  • 2,368
  • 2
  • 16
  • 19
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

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
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.

Renato Gama
  • 16,431
  • 12
  • 58
  • 92
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>
brso05
  • 13,142
  • 2
  • 21
  • 40
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);
        }
    }
}
Community
  • 1
  • 1
John Weisz
  • 30,137
  • 13
  • 89
  • 132
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);
Sandeep Pal
  • 2,067
  • 1
  • 16
  • 14