我有像这样的JSON集合对象
"Sections":[
{"SectionName":"Dallas", ...},
{"SectionName":"Austin", ...},
{"SectionName":"Housto", ...}
]
如果我想搜索字符串" Austin" - 如何在不循环的情况下直接搜索?
我这样做但不直接。
if (sections.length > 0) {
$.each(sections, function(myObject) {
var sectionObject = sections[myObject];
if (Object.keys(sectionObject).length > 0 && sectionObject["SectionName"] != undefined) {
...
}
});
答案 0 :(得分:2)
您可以使用Filter
并仅返回包含"SectionName": "Austin"
var data = {
"Sections": [{
"SectionName": "Dallas",
}, {
"SectionName": "Austin",
}, {
"SectionName": "Housto",
}]
}
data = data['Sections'].filter(el => el['SectionName'] == 'Austin');
console.log(data)