使用javascript将数组简化为特定结构

时间:2018-09-29 07:39:29

标签: javascript arrays object

我有一个对象数组

[
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    {"id":"diameter","label":"Diameter","value":""},
    {"id":"brand","label":"Brand","value":"value2"},
    {"id":"waterproof","label":"Waterproof","value":"value2"},
    {"id":"diameter","label":"Diameter","value":""}
]

我需要将其重组为以下结构,

[
    ["Brand", "value1", "value2"], 
    ["Waterproof", "value1","value2"], 
    ["Diameter","",""]
]

关于如何使用reduce方法做到这一点的任何想法。

最佳

6 个答案:

答案 0 :(得分:2)

必须取Map并收集所有值。

var json = '[{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}]',
    result = Array.from(JSON
        .parse(json)
        .reduce((m, { id, label, value }) => m.set(id, (m.get(id) || [label]).concat(value)), new Map)
        .values()
    );
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

请尝试使用Array.reduceObject.values

let arr = [{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}];

let result = Object.values(arr.reduce((a,c) => {
  if(a[c.id]) a[c.id].push(c.value);
  else a[c.id] = [c.label, c.value];
  return a;
}, {}));
console.log(result);

答案 2 :(得分:1)

不是最好的代码,但是我认为这可能对您有用:

let data = [
        {"id":"brand","label":"Brand","value":"value1"},    
        {"id":"waterproof","label":"Waterproof","value":"value1"},
        {"id":"diameter","label":"Diameter","value":""},
        {"id":"brand","label":"Brand","value":"value2"},
        {"id":"waterproof","label":"Waterproof","value":"value2"},
        {"id":"diameter","label":"Diameter","value":""}
    ]

    let reducedData = {}
    data.forEach((row)=>{
        reducedData[row.id] ? reducedData[row.id].push(row.value) : reducedData[row.id] = [row.label, row.value]    
    })
    let newData = Object.keys(reducedData).map((id)=>{return [...reducedData[id]]})

答案 3 :(得分:1)

您还可以使用Array.prototype.reduceObject.keys的简单组合:

const data = [{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}];

const grouped = data.reduce((o, { label, value }) =>
    ({...o, [label]: [...(o[label] || []), value]}), {});

const result = Object.keys(grouped).map(label => [label, ...grouped[label]]);

console.log(result);

答案 4 :(得分:1)

在这里,我使用Set来创建profile的唯一列表,然后我id来创建基于这些map的自定义数组。这是通过使用find来获取标签,并使用filter来获取与id相关的所有项目来完成的。我再次在过滤器上映射,以仅返回对象的id

value

答案 5 :(得分:0)

如果JSON数组是某个API中很长的数组,则

var data = [
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    ...... //some JSON array form some API
];
var json = JSON.stringify(data);

result = Array.from(JSON
    .parse(json)
    .reduce((m, { id, label, value }) => m.set(id, (m.get(id) || [label]).concat(value)), new Map)
    .values()
);
console.log(result);