12

I'm trying to filter an array in javascript, and am struggling when the array is nested.

At the moment, the furthest I've been able to get is filtering a flat array:

var ID = 3
var arr = [{ id : 1, name: "a" }, { id : 2, name: "b" }, { id : 3, name: "c" }]
var result = arr.filter(function( obj ) {return obj.id == ID;});
alert(result[0].name);

Though the above doesn't work if the array looks like this instead:

var arr2 = [
 [{ id : 1, name: "a" },{ id : 2, name: "b" }],
 [{ id : 3, name: "c" },{ id : 4, name: "d" }]
] 

The two examples can be found: https://jsfiddle.net/vjt45xv4/

Any tips for finding the appropriate result on the nested array would be much appreciated.

Thanks!

asked Sep 14, 2015 at 18:25
2
  • 2
    If the array looks like that instead, what do you want the result to be? (Or do you only ever want to find one object?) Commented Sep 14, 2015 at 18:28
  • Apply the same filter you are using on your arr to each item in arr2 (since items in arr2 are arrays themselves) Commented Sep 14, 2015 at 18:29

3 Answers 3

13

Flatten the array then filter it:

arr.reduce(function(a,b) { return a.concat(b); })
 .filter(function(obj) { return obj.id == ID; });
answered Sep 14, 2015 at 18:30
Sign up to request clarification or add additional context in comments.

Comments

3
arr2.filter(function(obj) {
 obj.filter(function(d) { 
 if(d.id == ID) { 
 result = d;
 }
 })
});
alert(result.name);

Hope this is what you were looking for. Rather than flattening the data here I went into the nested array till the point where it was flat(and matching) and set the result there.

arr2.forEach(function(d) {
 d.forEach(
 function(dd){ 
 alert(dd.id);
 if (dd.id==ID){
 result=dd; 
 }
 }
 );
});
alert(result.name);

Edit: As minitech mentioned its same working if just using forEach.

Full Stack Alien
12.7k2 gold badges28 silver badges39 bronze badges
answered Sep 14, 2015 at 18:49

2 Comments

If you’re going to assign to a variable, why not just use forEach?
Yep no argument there it can be done ! Besides I guess the way John Strickler mentioned is correct way if not looking for workaround.
0

I found this question from perhaps a similar problem:

I have a data structure such as this:

x.suites[0].specs[0].tests[0].results[0].status == "skipped"

My goal was to flag all of the non [skipped/ignored/???] status localed somewhere deep in this structure.

I came up with this solution:

var ignoreStatus = ["skipped"];
x.suites
.forEach( (xsuit)=> { xsuit.specs
 .forEach( (xspec)=> { xspec.tests
 .forEach( (xtst)=> { 
 var arry=xtst.results.filter((ftst)=>{ 
 return !ignoreStatus.includes(ftst.status)
 });
 if(arry.length>0){ 
 console.log(`!!!Flag this:${arry}`) 
 } else { 
 console.log("ignored:" ,xtst.results.map(m=>{return m.status}).join("/") ); 
 } 
 
 })
 })
}) 
answered Dec 26, 2021 at 8:38

Comments

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.