Filtering array of objects based on value

前端 未结 3 845
孤街浪徒
孤街浪徒 2021-01-21 12:49

Is there a way to filter an array of objects by a particular value that could be in any property?

Let\'s say I have this object:

var x = [
    {
                 


        
3条回答
  •  -上瘾入骨i
    2021-01-21 12:57

    Well the obvious thing is to combine your for..in loop with your .filter() function:

    var x = [{name: "one", swp: "two"}, {name: "two", swp: "three"}, { name: "aa", swp: "bb"}];
    
    var filtered = x.filter(function(v) {
      for (var k in v)
        if (v[k] === "two") return true;
    });
    console.log(filtered);

提交回复
热议问题