以与阵列查询对象(没有第三方脚本) [英] Query objects with an array (without 3rd party script)
本文介绍了以与阵列查询对象(没有第三方脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何查询一个数组对象?
myarray的无功= ['A','C']
使用myArray的,我怎么可以得到['1','3']一个返回数组了以下(previously JSON对象)(不使用第三方查询脚本 - 除非它是jQuery的:)
[{property1:1
property2:A
},
{property1:2,
property2:B的
},
{property1:3,
property2:C
}]
解决方案
您可以嵌套循环:
myarray的无功= ['A','C'];
变种JSON = [{
'property1':'1',
property2':'A'
},{
'property1':'2',
property2':'B'
},{
'property1':'3',
property2':'C'
}];VAR的结果=新的Array();
对于(VAR I = 0; I< myArray.length;我++){
对于(VAR J = 0; J< json.length; J ++){
如果(JSON [J] .property2 === myArray的[I]){
result.push(JSON [J] .property1);
}
}
}//在这一点上结果将包含['1','3']
How can you query an object with an array?
var myArray = ['A','C']
Using myArray, how can I get a returned array with ['1','3'] out of the following (previously JSON object) (without using a third party query script -- unless it's jQuery :)
[ { "property1": "1",
"property2": "A"
},
{ "property1": "2",
"property2": "B"
},
{ "property1": "3",
"property2": "C"
} ]
解决方案
You could a nested loop:
var myArray = ['A','C'];
var json = [{
'property1': '1',
'property2': 'A'
}, {
'property1': '2',
'property2': 'B'
}, {
'property1': '3',
'property2': 'C'
}];
var result = new Array();
for (var i = 0; i < myArray.length; i++) {
for (var j = 0; j < json.length; j++) {
if (json[j].property2 === myArray[i]) {
result.push(json[j].property1);
}
}
}
// at this point result will contain ['1', '3']
这篇关于以与阵列查询对象(没有第三方脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文