首页 > 解决方案 > 如何过滤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" }]

标签: arraysjson

解决方案


从一个新数组开始:

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' } ]

推荐阅读