简体   繁体   English

如何使用reduce合并JavaScript对象数组?

[英]How to merge JavaScript object array using reduce?

There's so many of post here regarding merging javaScript array objects relative to my question most of them are merging 2 arrays in my case it's different.这里有很多关于合并与我的问题相关的 javaScript 数组对象的帖子,其中大多数是合并 2 个数组,在我的情况下是不同的。

I have JSON from API I mapped and filtered them because i only need the 'tags'我有来自 API 的 JSON 我映射并过滤了它们,因为我只需要“标签”

Here's my code:这是我的代码:

JSON.map((key) => (key.tags))
.filter(function (element) {
    return element !== undefined;
})

Here's my result:这是我的结果:

   [ 
      { 
    "web_devt": { 
      "item_id": "858850847", 
      "tag": "web_devt" 
    } 
  }, 
  { 
    "web_devt": { 
      "item_id": "1004644524", 
      "tag": "web_devt" 
    } 
  }, 
  { 
    "pdo": { 
      "item_id": "1056300113", 
      "tag": "pdo" 
    }, 
    "php": { 
      "item_id": "1056300113", 
      "tag": "php" 
    }, 
    "web_devt": { 
      "item_id": "1056300113", 
      "tag": "web_devt" 
    } 
  }, 
  { 
    "parallax scrolling": { 
      "item_id": "1087678088", 
      "tag": "parallax scrolling" 
    } 
  }, 
  { 
    "react": { 
      "item_id": "2435593852", 
      "tag": "react" 
    } 
  }, 
  { 
    "javascript": { 
      "item_id": "2435595088", 
      "tag": "javascript" 
    } 
  }, 
  { 
    "react": { 
      "item_id": "2465629468", 
      "tag": "react" 
    } 
  } 
] 

I want to merge them like this:我想像这样合并它们:

[
{
"web_devt": [
    item_id: "765558416", 
        item_id: "765558416",
        item_id: "765558416",
        ...]
},
 {
"react": [
    item_id: "765558416",
    item_id: "765558416",
    item_id: "765558416",
    ...]
},
{
"JavaScipt": [
    item_id: "765558416",
    item_id: "765558416",
    item_id: "765558416",
    ...]
}

] ]

and so on ... Basically all the same tags will merge How you do this in reduce or any solution but not loadash or any third party?等等......基本上所有相同的标签都会合并你如何在reduce或任何解决方案中做到这一点,但不是loadash或任何第三方?

Your desired output syntax is invalid.您所需的输出语法无效。 You cannot have an array with keys nor you can have an object with multiple keys of the same name.你不能有一个带键的数组,也不能有一个带有多个同名键的对象。

However if you want have an array of ids, you could use reduce and group the data and for the individual item use Object.entries to get the key and the value within the object.但是,如果你想要一个 id 数组,你可以使用 reduce 和分组数据,对于单个项目使用Object.entries来获取对象中的键和值。

 var data = [ { "web_devt": { "item_id": "858850847", "tag": "web_devt" } }, { "web_devt": { "item_id": "1004644524", "tag": "web_devt" } }, { "pdo": { "item_id": "1056300113", "tag": "pdo" }, "php": { "item_id": "1056300113", "tag": "php" }, "web_devt": { "item_id": "1056300113", "tag": "web_devt" } }, { "parallax scrolling": { "item_id": "1087678088", "tag": "parallax scrolling" } }, { "react": { "item_id": "2435593852", "tag": "react" } }, { "javascript": { "item_id": "2435595088", "tag": "javascript" } }, { "react": { "item_id": "2465629468", "tag": "react" } } ] const res = data.reduce((acc, item) => { Object.entries(item).forEach((attr) => { const [key,value] = attr; if(value.tag !== undefined) { if (acc[key]){ acc[key].push(value.item_id); } else { acc[key] = [value.item_id]; } } }); return acc; }, {}) console.log(res);

Assuming that the inner arrays are meant to contain objects, use .reduce to group the input objects into an object indexed by item_id , then transform that object into an array using Object.entries :假设内部数组旨在包含对象,请使用.reduce将输入对象分组为item_id索引的对象,然后使用Object.entries将该对象转换为数组:

 const input = [ { "web_devt": { "item_id": "858850847", "tag": "web_devt" } }, { "web_devt": { "item_id": "1004644524", "tag": "web_devt" } }, { "pdo": { "item_id": "1056300113", "tag": "pdo" }, "php": { "item_id": "1056300113", "tag": "php" }, "web_devt": { "item_id": "1056300113", "tag": "web_devt" } }, { "parallax scrolling": { "item_id": "1087678088", "tag": "parallax scrolling" } }, { "react": { "item_id": "2435593852", "tag": "react" } }, { "javascript": { "item_id": "2435595088", "tag": "javascript" } }, { "react": { "item_id": "2465629468", "tag": "react" } } ]; const outputObj = input.reduce((a, outerObj) => { Object.entries(outerObj).forEach(([key, { item_id }]) => { if (!a[key]) a[key] = []; a[key].push({ item_id }); }); return a; }, {}); const output = Object.entries(outputObj).map(([key, arr]) => ({ [key]: arr })); console.log(output);

Here is my try using Array.reduce() with a nested Object.values() and Array.forEach() :这是我尝试将Array.reduce()与嵌套的Object.values()Array.forEach()

 const input = [ { "web_devt": {"item_id": "858850847", "tag": "web_devt"} }, { "web_devt": {"item_id": "1004644524", "tag": "web_devt"} }, { "pdo": {"item_id": "1056300113", "tag": "pdo"}, "php": {"item_id": "1056300113", "tag": "php"}, "web_devt": {"item_id": "1056300113", "tag": "web_devt"} }, { "parallax scrolling": {"item_id": "1087678088", "tag": "parallax scrolling"} }, { "react": { "item_id": "2435593852", "tag": "react"} }, { "javascript": {"item_id": "2435595088", "tag": "javascript"} }, { "react": {"item_id": "2465629468", "tag": "react"} } ]; let res = input.reduce((acc, obj) => { Object.values(obj).forEach(({item_id, tag}) => { acc[tag] = acc[tag] || {[tag]:[]} acc[tag][tag].push(item_id); }); return acc; }, {}); console.log(Object.values(res));
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper{max-height:100% !important; top:0}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM