过滤多维JSON数组
filter multi-dimension JSON arrays
以下是JSON详细信息:
var array={
"app": {
"categories": {
"cat_222": {
"id": "555",
"deals": [{
"id": "73",
"shop": "JeansWest"
},
{
"id": "8630",
"shop": "Adidas"
},
{
"id": "11912",
"shop": "Adidas"
}]
},
"cat_342": {
"id": "232",
"deals": [{
"id": "5698",
"shop": "KFC"
},
{
"id": "5701",
"shop": "KFC"
},
{
"id": "5699",
"shop": "MC"
}]
}
}
}
}
我已经尝试过过滤数组,使其具有包含da
模式的shop
。
var filted = _.filter(array.app.categories,function(item) {
return _.any(item.deals,function(c) {
return c.shop.indexOf('da') != -1;
});
});
===========更新==============================================
刚刚发现,这个代码是有效的。但它返回的内容是这样的:
[{
"id": "555",
"deals": [{
"id": "73",
"shop": "JeansWest"
}, {
"id": "8630",
"shop": "Adidas"
}, {
"id": "11912",
"shop": "Adidas"
}]
}]
理想情况下,我想要这样的东西:
[{
"id": "555",
"deals": [{
"id": "8630",
"shop": "Adidas"
}, {
"id": "11912",
"shop": "Adidas"
}]
}]
您可以执行此
var filted = _.reduce(array.app.categories, function (memo, item) {
var result = _.filter(item.deals, function (c) {
return c.shop.indexOf('da') != -1;
});
if (result.length > 0) {
var newResult = {};
newResult.deals = result;
newResult.id = item.id;
memo = _.union(memo, newResult);
}
return memo;
}, []);
相关文章:
- 为DataTables aoColumnDefs创建JavaScript数组(JSON格式)
- 使用 Javascript 将行添加到数组 json
- PHP数组JSON编码和该对象在ExtJs中解码
- 数组json转换为数组json
- 将Blob文本转换为Javascript中的数组/JSON对象
- 如何合并这些数组/json 对象
- 尝试从数组 JSON 键中删除引号
- JavaScript 中的数组 JSON
- 在数组 JSON 格式的 JavaScript 变量末尾添加新地址
- 如何访问这个Javascript数组(JSON对象?)
- rails传递记录数组(JSON)
- 按值提取对象/数组json vs数组
- 获取具有子数组json值的数组的值
- Object对象数组Json.字符串数组的字符串化
- 如何在Ext js 4.1中过滤数组json
- PHP -多维数组Json
- 正在分析数组Json以选择Tag
- 试图将字符串数据转换为数值数据,将数据放入数组的数组(Json)
- 从数组json、javascript中的specf值中删除引号
- 使用JavaScript或jQuery读取数组JSON