我有一个对象数组
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_focus
和difficulty
?这可能看起来很傻,但我已经被困了几个小时而无法找到解决方案
答案 0 :(得分:2)
你可以使用一个对象进行搜索,它提供了键和想要的值,比如
search = {
body_focus: "upper",
difficulty: "three"
}
然后遍历数组并使用实际对象的值检查search
的所有属性。如果所有搜索条件都匹配,则返回true
。
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;
答案 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)
尼娜的回答应该适合你。但如果你感兴趣,另一种方法是:
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;
答案 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);