[英]Filter array of objects against another object in Javascript
I have an array of objects 我有一系列对象
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"
}
]
I want to filter that array of objects against another object 我想针对另一个对象过滤该对象数组
var myObj = {
"upper": true,
"three": true
}
so now myObj
has a key "upper"
and "three"
and their values are true
. 所以现在
myObj
有一个键"upper"
和"three"
,它们的值是true
。 So based these values I want to make a function to get all the objects in the data
array that its key "body_focus"
has value of "upper"
and "difficulty"
key that has a value of "three"
因此,基于这些值,我想创建一个函数来获取
data
数组中的所有对象,其键"body_focus"
值为"upper"
, "difficulty"
键的值为"three"
so the function should return only these objects 因此该函数应仅返回这些对象
[
{
"body_focus": "upper",
"difficulty": "three",
"calories_min": "122",
"calories_max": "250"
},
{
"body_focus": "upper",
"difficulty": "three",
"calories_min": "150",
"calories_max": "280"
}
]
this is how I tried to approach the problem 这就是我试图解决问题的方式
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;
}
}
}
my code above only checks for the key body_focus
, so how can I check for both body_focus
and difficulty
? 我上面的代码仅检查
body_focus
,那么如何同时检查body_focus
和difficulty
? it might seem silly but I've been stuck for hours and can't find a solution 可能看起来很傻,但是我被困了几个小时,找不到解决方案
You could use an object for searching which gives the keys and the wanted values, like 您可以使用一个对象进行搜索,该对象给出键和所需的值,例如
search = {
body_focus: "upper",
difficulty: "three"
}
then iterate through the array and check all properties of search
with the values of the actual object. 然后遍历数组,并使用实际对象的值检查
search
所有属性。 Return true
if all search criteria matches. 如果所有搜索条件都匹配,则返回
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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This should do the trick I think. 我认为这应该可以解决问题。
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);
}
}
With myObj[entry.body_focus] you are checking if the myObj has the property upper and if it's true. 使用myObj [entry.body_focus]来检查myObj是否具有upper属性,以及它是否为true。 Same with difficulty.
同样有困难。
This solution is using a combination of Array.prototype.filter
and Array.prototype.every
to match all the values in your data object with the keys in the param object. 此解决方案使用
Array.prototype.filter
和Array.prototype.every
的组合,以将数据对象中的所有值与param对象中的键匹配。
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 params = { "upper": true, "three": true } function filter(params, data) { const keys = Object.keys(params) return data.filter(item => keys.every(value => ~Object.values(item).indexOf(value)) ) } console.log( filter(params, data) )
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
Nina's answer should work for you. 妮娜的答案应该对您有用。 But in case you are interested, another way to do it:
但是,如果您有兴趣,可以使用另一种方法:
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);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
it would be a good idea to change your myObj
object's structure to be more of 最好将
myObj
对象的结构更改为更多
let myObj =
{
body_focus: "upper",
difficulty: "three"
}
you can then use filter
on your original array of objects to get what you need. 然后,您可以对原始对象数组使用
filter
,以获取所需的内容。
let results = data.filter(item => item.body_focus === myObj.body_focus && item.difficulty === myObj.difficulty);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.