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!

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; });
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.

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("/") ); 
                } 
                
        })
    })
}) 


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.