arrays - 如何过滤json以组合结果
问题描述
我有一个json,如下所示,我需要先将其组合起来,然后才能在我的代码中使用它
我尝试过数组原型,但只能过滤产品 ID 或城市,
question = [{ store_id: 489, product_id: "empty", city: "Delhi" },
{ store_id: 472, product_id: "empty", city: "Delhi" },
{ store_id: 489, product_id: 123123, city: "empty" },
{ store_id: 472, product_id: 456456, city: "empty" }]
var newArray = question.filter(function (el) {
return el.store_id != "empty" &&
el.city != "empty"
});
console.log(newArray);
我需要根据 store_id 组合对象并获取更新的 json,其中 product_id 或 city 中没有“空”
answer = [{ store_id: 489, product_id: 123123, city: "Delhi" },
{ store_id: 472, product_id: 456456, city: "Delhi" }]
解决方案
从一个新数组开始:
var answer = [];
然后用源数组中的对象填充它,只复制我们想要保留的属性:
var answer = [];
for (let store of question)
{
let newEntry = {};
for (let prop in store)
{
if ( store[prop] !== "empty" ) newEntry[prop] = store[prop];
}
answer.push(newEntry);
}
console.log(answer);
输出:
0: Object { store_id: 489, city: "Delhi" }
1: Object { store_id: 472, city: "Delhi" }
2: Object { store_id: 489, product_id: 123123 }
3: Object { store_id: 472, product_id: 456456 }
length: 4
这是一个开始 - 没有空属性。但也没有合并。所以现在我们可以添加:在我们的结果中查找具有正确 store_id 的现有条目,并将非空属性合并到其中:
var answer = [];
for (let store of question)
{
let newEntry = {};
for (let prop in store)
{
if ( store[prop] !== "empty" ) newEntry[prop] = store[prop];
}
let existing = answer.find(s => s.store_id === store.store_id);
if ( existing )
Object.assign(existing, newEntry);
else
answer.push(newEntry);
}
console.log(answer);
输出:
0: Object { store_id: 489, city: "Delhi", product_id: 123123 }
1: Object { store_id: 472, city: "Delhi", product_id: 456456 }
length: 2
您可能想要添加的最后一步,以过滤掉任何可能尚未完全填充的对象 - 但您的示例中没有:
answer = answer.filter(s => s.city && s.product_id);
一起:
var question = [{ store_id: 489, product_id: "empty", city: "Delhi" },
{ store_id: 472, product_id: "empty", city: "Delhi" },
{ store_id: 489, product_id: 123123, city: "empty" },
{ store_id: 472, product_id: 456456, city: "empty" }];
var answer = [];
for (let store of question)
{
let newEntry = {};
for (let prop in store)
{
if ( store[prop] !== "empty" ) newEntry[prop] = store[prop];
}
let existing = answer.find(s => s.store_id === store.store_id);
if ( existing )
Object.assign(existing, newEntry);
else
answer.push(newEntry);
}
answer = answer.filter(s => s.city && s.product_id);
console.log(answer);
现在您有了一个可行的解决方案,但效率不高;如果您有大量数据要处理,您将希望一次完成尽可能多的处理,就像 David Winder 的这个版本一样:
question = [{ store_id: 489, product_id: "empty", city: "Delhi" },
{ store_id: 472, product_id: "empty", city: "Delhi" },
{ store_id: 489, product_id: 123123, city: "empty" },
{ store_id: 472, product_id: 456456, city: "empty" }];
let ans = {};
for (let key in question) {
let value = question[key]; // get the value by key
if (value.store_id in ans) { // if already exist
if (ans[value.store_id].product_id == "empty") // update product_id
ans[value.store_id].product_id = value.product_id;
if (ans[value.store_id].city == "empty") // update city
ans[value.store_id].city = value.city;
} else
ans[value.store_id] = value;
}
console.log(Object.keys(ans).map(i => ans[i]));
这将输出:
[ { store_id: 472, product_id: 456456, city: 'Delhi' },
{ store_id: 489, product_id: 123123, city: 'Delhi' } ]
推荐阅读
- python - 如何制作具有不同散点大小和颜色的散点图,对应于数据框中的一系列值?
- angular - ngrx 动作:如何测试该动作已被调度
- php - Laravel 发布 302 重定向
- python - XGBRegressor + cross_val_score 只使用 1 个内核?
- css - 如何使用 css hoverbox 制作过渡动画?
- ipfs - 是否需要集群来固定 ipfs 专用网络中的文件?
- android - 如何向 AOSP 添加自定义 INTENT 以授予对系统级控制调用的访问权限?
- drools - 如何在 OWL 本体上运行 Drools 规则引擎(KIE)?
- odata - ADF - Odata - 如何执行可选选择
- kotlin - Kotlin:多个命名的伴随对象