简体   繁体   English

"如何使用来自平面JSON的第一个条目作为javascript中的父项创建嵌套JSON"

[英]How to create nested JSON using first entry from from flat JSON as parent in javascript

I've tried various answers to this (same\/similar) question here, and none have given the desired result.我在这里尝试了这个(相同\/相似)问题的各种答案,但没有一个给出预期的结果。 I have a flat Json file that I need to convert to nested.我有一个需要转换为嵌套的平面 Json 文件。 The first item in each chunk is the item that the data needs grouping on when converted to nested data.每个块中的第一项是数据在转换为嵌套数据时需要分组的项。 The example data just shows two "types" but there are several more in the data, and a few more other elements along with colour and size as well - I kept it short for posting here.示例数据只显示了两种“类型”,但数据中还有更多,还有一些其他元素以及颜色和大小 - 我在此处发布时保持简短。

Example data input:示例数据输入:

[
  {
    "type": "Background",
    "colour": "Blue",
    "size": "5"
  },
  {
    "type": "Background",
    "colour": "Grey",
    "size": "3"
  },
  {
    "type": "Foreground",
    "colour": "Red",
    "size": "5"
  },
  {
    "type": "Foreground",
    "colour": "White",
    "size": "10"
  }
]

You can use the forEach()<\/code> method to loop over the array, in this case, result<\/em> .您可以使用forEach()<\/code>方法循环遍历数组,在本例中为result<\/em> 。 If the type wasn't set in the result<\/em> , then add the new type.如果结果<\/em>中未设置类型,则添加新类型。 But, if the item already has its type in the result<\/em> , then just push the item's elements.但是,如果 item 已经在result<\/em>中有它的类型,那么只需 push item 的元素。

 const items = [ { type: 'Background', colour: 'Blue', size: '5', }, { type: 'Background', colour: 'Grey', size: '3', }, { type: 'Foreground', colour: 'Red', size: '5', }, { type: 'Foreground', colour: 'White', size: '10', }, ]; let result = []; items.forEach((item) => { if (!result.find((x) => x.type === item.type)) { result.push({ type: item.type, elements: [] }); } result .find((x) => x.type === item.type) .elements.push({ color: item.colour, size: item.size, }); }); console.log(result);<\/code><\/pre>

https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/forEach<\/a><\/em> https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/forEach<\/a><\/em>

"

It seems that there are two steps here.这里似乎有两个步骤。 First group all entries with the same type (which would be easier under the type name as a property, then build the nesting.首先将具有相同类型的所有条目分组(在类型名称下作为属性会更容易,然后构建嵌套。

 const input = [ { "type": "Background", "colour": "Blue", "size": "5" }, { "type": "Background", "colour": "Grey", "size": "3" }, { "type": "Foreground", "colour": "Red", "size": "5" }, { "type": "Foreground", "colour": "White", "size": "10" } ]; var assist = {}, output = []; \/\/ Group original objects based on type \/\/ The type is used as a property value for (let i = 0; i < input.length; i++) { let type = input[i].type; if (assist.hasOwnProperty(type)) { assist[type].push(input[i]); } else { assist[type] = [input[i]]; } delete input[i].type; } \/\/ Turn the grouped entries into new array entries let keys = Object.keys(assist); for (let i = 0; i < keys.length; i++) { output.push({ type: keys[i], elements: assist[keys[i]] }); }; \/\/ Show results console.log(output);<\/code><\/pre>

Note that while the above code does not modify the original array, it does modify it's members (deleting their type<\/code> property) and using them in the output array.请注意,虽然上面的代码没有修改原始数组,但它确实修改了它的成员(删除它们的type<\/code>属性)并在输出数组中使用它们。 You will have to clone it if you need the original too.如果您也需要原件,则必须克隆它。

"

try this试试这个

var output = [];

var newObj = { type: input[0].type, elements: [] };
var prevType = input[0].type;

input.forEach((element) => {
  if (element.type != prevType) {
    output.push(newObj);
    newObj = { type: element.type, elements: [] };
    prevType = element.type;
  }
  newObj.elements.push({ colour: element.colour, size: element.size });
});
output.push(newObj);

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

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