简体   繁体   English

简单的JavaScript转换JSON格式

[英]Simple JavaScript Convert JSON format

I have a json format and want to convert 我有json格式,想转换 从左到右

Here are my script. 这是我的剧本。 I had tried but cannot get the correct results. 我曾经尝试过,但无法获得正确的结果。 Please give some advice, thanks and appreciate. 请给一些建议,谢谢和赞赏。

 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(); 

One option is to reduce into an object indexed by pid s, whose values are arrays. 一种选择是reduce为由pid索引的对象,该对象的值为数组。 On each iteration, create the array at the appropriate property if it doesn't exist, and then push to that array: 在每次迭代中,如果不存在,请在适当的属性处创建数组,然后推送到该数组:

 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; }, {}) ); 

You're pretty close there. 您就在那附近。 But [] is to initialize an array instead of an object in javascript. 但是[]是要初始化数组,而不是javascript中的对象。 In JS, it's {} . 在JS中,它是{}

Following is one of many ways you can accomplish this. 以下是可以完成此操作的许多方法之一。

 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(); 

Use this method for your any group by 对您的任何组使用此方法

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');

You pass your key as second argument into groupBy for group by. 您将密钥作为第二个参数传递给groupBy进行分组依据。

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

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