4

I would like to know how to change array object to nested object in javascript. I have list as array object, how to convert to nested 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

{
  "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"}
}
7
  • I made a snippet - moving the call to AFTER the objects stopped the error Commented Apr 3, 2020 at 9:05
  • You pass list to nestedobj() before you define it, so it's undefined Commented Apr 3, 2020 at 9:05
  • @ChayimFriedman that is just one issue - the output is not according to spec Commented Apr 3, 2020 at 9:06
  • you are passing list both times Commented Apr 3, 2020 at 9:07
  • @JaromandaX My bad. Fixed Commented Apr 3, 2020 at 9:07

3 Answers 3

5

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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Pranav C Balan, thanks for reply, how to convert the result nested object to array i.e get the output as input , tried let convertobj = Object.keys(listoutput).map(function(k) { return listouput[k] })
@miyavv : Object.values(listoutput) is also fine
0

// 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}

Comments

0

If the number after list is the ordinal position of the item plus one

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

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.