如何在JavaScript中将数组对象转换为嵌套对象

4
我想知道如何在JavaScript中将数组对象转换为嵌套对象。
我有一个名为list的数组对象,如何将其转换为嵌套对象。

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)

预期输出

{
  "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"}
}

我创建了一个代码片段 - 将调用移动到对象停止之后解决了错误。 - mplungjan
你在定义 list 之前就将其传递给了 nestedobj(),因此它未被定义。 - Chayim Friedman
@ChayimFriedman 这只是一个问题 - 输出不符合规范。 - mplungjan
你两次都传递了列表。 - Jaromanda X
在任何情况下,您只需要处理两个项目。 - mplungjan
显示剩余2条评论
3个回答

5
你可以使用Array#reduce方法(或简单循环)生成对象,其中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));


@Pranav C Balan,感谢您的回复,如何将结果嵌套对象转换为数组,即获取输出作为输入,尝试使用let convertobj = Object.keys(listoutput).map(function(k) { return listouput[k] }) - miyavv
@miyavv:Object.values(listoutput) 也可以。 - Pranav C Balan

0

// 循环和收集:

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}


0

如果list后面的数字是该项的序数加一

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)

然而,如果列表后面的数字与对象的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)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接