搜索深层嵌套
Search deep nested
我正在研究一个解决方案,我需要通过 id
在深度嵌套的 JSON 中搜索元素。有人建议我使用 underscore.js,我对它还很陌生。
阅读文档 http://underscorejs.org/#find 后,我尝试使用 find
、filter
和 findWhere
来实现解决方案。
这是我尝试使用 find
的结果:
var test = {
"menuInputRequestId": 1,
"catalog":[
{
"uid": 1,
"name": "Pizza",
"desc": "Italian cuisine",
"products": [
{
"uid": 3,
"name": "Devilled chicken",
"desc": "chicken pizza",
"prices":[
{
"uid": 7,
"name": "regular",
"price": ""
},
{
"uid": 8,
"name": "large",
"price": ""
}
]
}
]
},
{
"uid": 2,
"name": "Pasta",
"desc": "Italian cuisine pasta",
"products": [
{
"uid": 4,
"name": "Lasagne",
"desc": "chicken lasage",
"prices":[
{
"uid": 9,
"name": "small",
"price": ""
},
{
"uid": 10,
"name": "large",
"price": ""
}
]
},
{
"uid": 5,
"name": "Pasta",
"desc": "chicken pasta",
"prices":[
{
"uid": 11,
"name": "small",
"price": ""
},
{
"uid": 12,
"name": "large",
"price": ""
}
]
}
]
}
]
};
var x = _.find(test, function (item) {
return item.catalog && item.catalog.uid == 1;
});
还有一个Fiddlehttp://jsfiddle.net/8hmz0760/
我遇到的问题是这些函数检查结构的顶层而不是嵌套属性,因此返回 undefined
。我尝试使用类似问题 Underscore.js - filtering in a nested Json 中建议的 item.catalog && item.catalog.uid == 1;
逻辑,但失败了。
如何通过搜索整个深层嵌套结构按值查找项目?
编辑:
以下代码是我尝试过的最新代码。问题在于它直接遍历到 prices 嵌套对象并试图找到值。但是我的要求是在JSON.
的所有层中搜索值
var x = _.filter(test, function(evt) {
return _.any(evt.items, function(itm){
return _.any(itm.outcomes, function(prc) {
return prc.uid === 1 ;
});
});
});
我不使用 underscore.js 但你可以使用这个
function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}
function find(json,key,value){
var result = [];
for (var property in json)
{
//console.log(property);
if (json.hasOwnProperty(property)) {
if( property == key && json[property] == value)
{
result.push(json);
}
if( isArray(json[property]))
{
for(var child in json[property])
{
//console.log(json[property][child]);
var res = find(json[property][child],key,value);
if(res.length >= 1 ){
result.push(res);}
}
}
}
}
return result;
}
console.log(find(test,"uid",4));
这是一个创建对象的解决方案,其中的键是 uid:
var catalogues = test.catalog;
var products = _.flatten(_.pluck(catalogues, 'products'));
var prices = _.flatten(_.pluck(products, 'prices'));
var ids = _.reduce(catalogues.concat(products,prices), function(memo, value){
memo[value.uid] = value;
return memo;
}, {});
var itemWithUid2 = ids[2]
var itemWithUid12 = ids[12]
我正在研究一个解决方案,我需要通过 id
在深度嵌套的 JSON 中搜索元素。有人建议我使用 underscore.js,我对它还很陌生。
阅读文档 http://underscorejs.org/#find 后,我尝试使用 find
、filter
和 findWhere
来实现解决方案。
这是我尝试使用 find
的结果:
var test = {
"menuInputRequestId": 1,
"catalog":[
{
"uid": 1,
"name": "Pizza",
"desc": "Italian cuisine",
"products": [
{
"uid": 3,
"name": "Devilled chicken",
"desc": "chicken pizza",
"prices":[
{
"uid": 7,
"name": "regular",
"price": ""
},
{
"uid": 8,
"name": "large",
"price": ""
}
]
}
]
},
{
"uid": 2,
"name": "Pasta",
"desc": "Italian cuisine pasta",
"products": [
{
"uid": 4,
"name": "Lasagne",
"desc": "chicken lasage",
"prices":[
{
"uid": 9,
"name": "small",
"price": ""
},
{
"uid": 10,
"name": "large",
"price": ""
}
]
},
{
"uid": 5,
"name": "Pasta",
"desc": "chicken pasta",
"prices":[
{
"uid": 11,
"name": "small",
"price": ""
},
{
"uid": 12,
"name": "large",
"price": ""
}
]
}
]
}
]
};
var x = _.find(test, function (item) {
return item.catalog && item.catalog.uid == 1;
});
还有一个Fiddlehttp://jsfiddle.net/8hmz0760/
我遇到的问题是这些函数检查结构的顶层而不是嵌套属性,因此返回 undefined
。我尝试使用类似问题 Underscore.js - filtering in a nested Json 中建议的 item.catalog && item.catalog.uid == 1;
逻辑,但失败了。
如何通过搜索整个深层嵌套结构按值查找项目?
编辑:
以下代码是我尝试过的最新代码。问题在于它直接遍历到 prices 嵌套对象并试图找到值。但是我的要求是在JSON.
的所有层中搜索值var x = _.filter(test, function(evt) {
return _.any(evt.items, function(itm){
return _.any(itm.outcomes, function(prc) {
return prc.uid === 1 ;
});
});
});
我不使用 underscore.js 但你可以使用这个
function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}
function find(json,key,value){
var result = [];
for (var property in json)
{
//console.log(property);
if (json.hasOwnProperty(property)) {
if( property == key && json[property] == value)
{
result.push(json);
}
if( isArray(json[property]))
{
for(var child in json[property])
{
//console.log(json[property][child]);
var res = find(json[property][child],key,value);
if(res.length >= 1 ){
result.push(res);}
}
}
}
}
return result;
}
console.log(find(test,"uid",4));
这是一个创建对象的解决方案,其中的键是 uid:
var catalogues = test.catalog;
var products = _.flatten(_.pluck(catalogues, 'products'));
var prices = _.flatten(_.pluck(products, 'prices'));
var ids = _.reduce(catalogues.concat(products,prices), function(memo, value){
memo[value.uid] = value;
return memo;
}, {});
var itemWithUid2 = ids[2]
var itemWithUid12 = ids[12]