使用Javascript:过滤二维数组 [英] Javascript: Filtering twodimensional array
问题描述
有一个关于如何根据它的数值进行排序的JavaScript数组提供充足的例子。然而,什么是与物业为prop1
与它的根据值<$ C $来从 myArray的
所有元素适当的方式C>值1 ?
There's plenty examples available on how to sort an javascript array based on it's numeric values. What would however be appropriate way to fetch all elements from myArray
with the property prop1
with it's according value value1
?
下面是我的数组:
var myArray = [
{
"id":"2",
"name":"My name",
"properties":{"prop1":"value1"}
}];
感谢
推荐答案
您可以通过点或括号标记只是访问和匹配成员推到新的/滤波阵列,例如:
You can just access it by dot or bracket notation and push the matching members to your new/filtered array, for example:
var newArray = [];
for(var i=0, l = myArray.length; i<l; i++) {
if(myArray[i].properties.prop1 == "value1") newArray.push(myArray[i]);
}
您的问题是一个有点暧昧不过,如果你想获得 {为prop1:值1}
对象,而不是父母,然后就变 newArray.push(myarray的[I])
到 newArray.push(myarray的[I]的.properties)
。
Your question is a bit ambiguous though, if you're trying to get the {"prop1":"value1"}
object, not the parent, then just change newArray.push(myArray[i])
to newArray.push(myArray[i].properties)
.
这篇关于使用Javascript:过滤二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!