简单的JavaScript转换JSON格式

时间:2018-10-23 07:16:51

标签: javascript json

我有一个json格式,想转换from left to right

这是我的剧本。我曾经尝试过,但无法获得正确的结果。请提供一些建议,谢谢和赞赏。

function groupBy() {
var list = [{
        "id": "009",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "007",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "006",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "pm1",
        "Nm": "Model 1",
        "pid": "puma"
    },
    {
        "id": "003",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "pm5",
        "Nm": "Model 1",
        "pid": "puma"
    },
    {
        "id": "aj1",
        "Nm": "Model 1",
        "pid": "nike"
    },
    {
        "id": "aj2",
        "Nm": "Model 1",
        "pid": "nike"
    }
];
var output = [];
for (var i = 0; i < list.length; i++) {
    if (list[i].pid != undefined) {
        output.push(list[i]);
    }
}
console.log(output);
}
groupBy();

3 个答案:

答案 0 :(得分:5)

一种选择是将reduce插入由pid索引的对象,该对象的值为数组。在每次迭代中,如果不存在,请在适当的属性处创建数组,然后推送到该数组:

var list = [
  {"id":"009","Nm":"Model 1","pid":"adidas"},
  {"id":"007","Nm":"Model 1","pid":"adidas"},
  {"id":"006","Nm":"Model 1","pid":"adidas"},
  {"id":"pm1","Nm":"Model 1","pid":"puma"},
  {"id":"003","Nm":"Model 1","pid":"adidas"},
  {"id":"pm5","Nm":"Model 1","pid":"puma"},
  {"id":"aj1","Nm":"Model 1","pid":"nike"},
  {"id":"aj2","Nm":"Model 1","pid":"nike"}
];
console.log(
  list.reduce((a, item) => {
    const { pid } = item;
    if (!a[pid]) a[pid] = [];
    a[pid].push(item);
    return a;
  }, {})
);

答案 1 :(得分:1)

您在那里很近。但是[]是要初始化数组而不是javascript中的对象。在JS中,它是{}

遵循是您可以完成此操作的众多方法之一。

function groupBy() {
    var list = [
        {"id":"009","Nm":"Model 1","pid":"adidas"},
        {"id":"007","Nm":"Model 1","pid":"adidas"},
        {"id":"006","Nm":"Model 1","pid":"adidas"},
        {"id":"pm1","Nm":"Model 1","pid":"puma"},
        {"id":"003","Nm":"Model 1","pid":"adidas"},
        {"id":"pm5","Nm":"Model 1","pid":"puma"},
        {"id":"aj1","Nm":"Model 1","pid":"nike"},
        {"id":"aj2","Nm":"Model 1","pid":"nike"}
    ];

    // Initialize output as an object
    var output = {};

    for (var i = 0; i < list.length; i++){
        // 'objectKey' is where you group the list item by its 'pid'
        var objectKey = list[i].pid;

        // If there's a 'pid' in the list item, but 'output' is not an array yet, then..
        if (objectKey && !output.hasOwnProperty(objectKey)){
            // Initialize output.group to be an array
            output[ objectKey ] = [];
        }

        // Then finally, store the list into output's group that we created above.
        output[ objectKey ].push( list[i] );
    }

    console.log(output);
}               

groupBy();

答案 2 :(得分:0)

将此方法用于您的任何组

const groupBy = function(arr, prop) {
  return arr.reduce(function(groups, item) {
    const val = item[prop]
    groups[val] = groups[val] || []
    groups[val].push(item)
    return groups
  }, {})
}

const list = [
                {"id":"009","Nm":"Model 1","pid":"adidas"},
                {"id":"007","Nm":"Model 1","pid":"adidas"},
                {"id":"006","Nm":"Model 1","pid":"adidas"},
                {"id":"pm1","Nm":"Model 1","pid":"puma"},
                {"id":"003","Nm":"Model 1","pid":"adidas"},
                {"id":"pm5","Nm":"Model 1","pid":"puma"},
                {"id":"aj1","Nm":"Model 1","pid":"nike"},
                {"id":"aj2","Nm":"Model 1","pid":"nike"}
            ];

const groupOutput = groupBy(list, 'pid');

您将密钥作为第二个参数传递给groupBy进行分组依据。