在Javascript中针对另一个对象过滤对象数组

时间:2016-12-02 13:48:53

标签: javascript object

我有一个对象数组

var data = [
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "122",
      "calories_max": "250"
    },
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "150",
      "calories_max": "280"
    },
    {
      "body_focus": "lower",
      "difficulty": "two",
      "calories_min": "100",
      "calories_max": "180"
    },
    {
      "body_focus": "total",
      "difficulty": "four",
      "calories_min": "250",
      "calories_max": "350"
    }
]

我想要针对另一个对象过滤该对象数组

var myObj = {
    "upper": true,
    "three": true
}

所以现在myObj有一个键"upper""three",其值为true。因此,基于这些值,我想创建一个函数来获取data数组中的所有对象,其键"body_focus"的值为"upper",而"difficulty"键具有值"three"

所以函数应该只返回这些对象

[
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "122",
      "calories_max": "250"
    },
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "150",
      "calories_max": "280"
    }
]

这就是我试图解决问题的方法

var entry;
var found = [];
for (var i = 0; i < data.length; i++) {
    entry = data[i];
    for(var key in myObj) {
        if(entry.body_focus.indexOf(key) !== -1) {
            found.push(entry);
            break;  
        }
    }
}

我上面的代码只检查密钥body_focus,那么如何检查body_focusdifficulty?这可能看起来很傻,但我已经被困了几个小时而无法找到解决方案

4 个答案:

答案 0 :(得分:2)

你可以使用一个对象进行搜索,它提供了键和想要的值,比如

search = {
    body_focus: "upper",
    difficulty: "three"
}

然后遍历数组并使用实际对象的值检查search的所有属性。如果所有搜索条件都匹配,则返回true

&#13;
&#13;
var data = [{ body_focus: "upper", difficulty: "three", calories_min: "122", calories_max: "250" }, { body_focus: "upper", difficulty: "three", calories_min: "150", calories_max: "280" }, { body_focus: "lower", difficulty: "two", calories_min: "100", calories_max: "180" }, { body_focus: "total", difficulty: "four", calories_min: "250", calories_max: "350" }],
    search = { body_focus: "upper", difficulty: "three" },
    result = data.filter(function (o) {
        return Object.keys(search).every(function (k) {
            return o[k] === search[k];
        });
    });

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这应该是我认为的伎俩。

var myObj = {
    "upper": true,
    "three": true
}
var found = [];
for (var i = 0; i < data.length; i++) {
    entry = data[i];
    if(myObj[entry.body_focus] && myObj[entry.difficulty]) {
      found.push(entry);
    }
}

使用myObj [entry.body_focus],您正在检查myObj是否具有属性upper以及是否为true。同样困难。

答案 2 :(得分:0)

尼娜的回答应该适合你。但如果你感兴趣,另一种方法是:

&#13;
&#13;
var data = [{
  "body_focus": "upper",
  "difficulty": "three",
  "calories_min": "122",
  "calories_max": "250"
}, {
  "body_focus": "upper",
  "difficulty": "three",
  "calories_min": "150",
  "calories_max": "280"
}, {
  "body_focus": "lower",
  "difficulty": "two",
  "calories_min": "100",
  "calories_max": "180"
}, {
  "body_focus": "total",
  "difficulty": "four",
  "calories_min": "250",
  "calories_max": "350"
}];

var search = { 
  body_focus: "upper", difficulty: "three" 
};

var results=$.grep(data,function(datum,index){
  var sat=true;
  Object.keys(search).forEach(function(el){
    if(datum[el]!=search[el])
      {
        sat=false;
        return false;
      }
  });
  return sat;
});
console.log(results);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;
关键是你可以使用$ .grep编写一个自定义过滤器函数,并使用第一个参数&#39; datum&#39;来检查一些条件。或者第二个索引&#39;并返回true表示过滤器匹配,或返回false表示否则

答案 3 :(得分:0)

myObj对象的结构更改为更好的结构是个好主意

let myObj = 
{
    body_focus: "upper",
    difficulty: "three"
}     

作为您的搜索对象。 (正如其中一个答案中提到的那样)

然后,您可以在原始对象数组上使用filter来获取所需内容。

let results = data.filter(item => item.body_focus === myObj.body_focus 
                               && item.difficulty === myObj.difficulty);