简体   繁体   English

如何在 javascript 中将数组 object 更改为嵌套的 object

[英]How to change array object to nested object in javascript

I would like to know how to change array object to nested object in javascript.我想知道如何将数组 object 更改为 javascript 中的嵌套 object。 I have list as array object, how to convert to nested objectlist了数组 object,如何转换为嵌套的 object

 function nestedobj(arrlist){ var result ={}; result.list1 = arrlist[0]; result.list2 = arrlist[1] return list; } var list= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"} ] var list1= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"}, {id: 3, cn: "MY"} ] var listobj = this.nestedobj(list); var listobj1 = this.nestedobj(list1); console.log(listobj) console.log(listobj1)

Expected Output预计 Output

{
  "list1":{"id": 1, "cn": "SG"},
  "list2":{"id": 2, "cn": "TH"}
}

{
  "list1":{"id": 1, "cn": "SG"},
  "list2":{"id": 2, "cn": "TH"},
  "list3":{"id": 3, "cn": "MY"}
}

You can use Array#reduce method(or simple loop) to generate the object where id property or index can be used to generate the property name.您可以使用Array#reduce方法(或简单循环)生成 object ,其中id属性或索引可用于生成属性名称。

 var list = [{id: 1, cn: "SG" }, { id: 2, cn: "TH" }] var list1 = [{ id: 1, cn: "SG" }, { id: 2, cn: "TH" }, { id: 3, cn: "MY" }] function convert(arr) { // iterate over the array return arr.reduce((obj, o, i) => { // define the property obj[`list${o.id}`] = o; // or with index obj[`list${i + 1}`] = o; // return object reference for next call return obj; // set initial value as empty object to keep the result }, {}) } // or with simple loop function convert1(arr) { const result = {}; for (let o of arr) result[`list${o.id}`] = o; return result } console.log(convert(list)); console.log(convert(list1)); console.log(convert1(list)); console.log(convert1(list1));

// Loop an collect: // 循环收集:

 var list = [ { id: 1, cn: "SG" }, { id: 2, cn: "TH" } ]; var list1 = [ { id: 1, cn: "SG" }, { id: 2, cn: "TH" }, { id: 3, cn: "MY" } ]; function nestedobj2(list) { let result = {}; for (const v of list) { result["list" + v.id] = v; } return result; } var listobj = nestedobj2(list); console.log("%j", listobj); listobj = nestedobj2(list1); console.log("%j", listobj); function nestedobj(list) { return list.reduce((x, data) => { x["list" + data.id] = data; return x; }, {}); } listobj = nestedobj(list); console.log("%j", listobj) listobj = nestedobj(list1); console.log("%j", listobj)
 .as-console-row {color: blue!important}

If the number after list is the ordinal position of the item plus one如果list后面的数字是项目的序数 position 加一

 var nestedobj = a => Object.fromEntries(Object.entries(a).map(([k, v]) => [`list${+k+1}`, v])); var list= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"} ] var list1= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"}, {id: 3, cn: "MY"} ] var listobj = nestedobj(list); var listobj1 = nestedobj(list1); console.log(listobj) console.log(listobj1)

If however, the number after list relates to the id of the object但是,如果列表后面的数字与 object 的 id 相关

 var nestedobj = a => Object.fromEntries(Object.values(a).map(v => [`list${v.id}`, v])); var list= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"} ] var list1= [ {id: 1, cn: "SG"}, {id: 2, cn: "TH"}, {id: 3, cn: "MY"} ] var listobj = nestedobj(list); var listobj1 = nestedobj(list1); console.log(listobj) console.log(listobj1)

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

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