简体   繁体   中英

How to create single object with multiple keys and elements?

I have one array with key and its array elements

When I am converting it from array to object then I am getting index 0 and 1 but here I need index should be array1 and array2

 let input = [{ "array1": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" }] }, { "array2": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" } ] } ]; function convert(arr) { let obj = new Object(); arr.forEach((value, key) => { obj = value; }); return obj; } console.log(convert(input)) 

so after calling this function i am getting following output

{
  "array2": [
     {
      "id": 1,
      "name": "Test 1"
     },
    {
      "id": 2,
      "name": "Test 2"
    }
  ]
}

but here I need output should be like this

{
  "array1": [
    {
      "id": 1,
      "name": "Test 1"
    },
    {
      "id": 2,
      "name": "Test 2"
    }
  ],
  "array2": [
    {
      "id": 1,
      "name": "Test 1"
    },
    {
      "id": 2,
      "name": "Test 2"
    }
  ]
}

If I use the define the array inside the convert function and push it then again i am getting the index 0 and 1.

How can i get expected result here.

For getting a single object with the wanted keys, you could assign all items to a single object with Object.assign with spread syntax ... .

 let input = [{ array1: [{ id: 1, name: "Test 1" }, { id: 2, name: "Test 2" }] }, { array2: [{ id: 1, name: "Test 1" }, { id: 2, name: "Test 2" }] }], object = Object.assign(...input); console.log(object) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 


Your code does not work because you take the latest assignment as a result

function convert(arr) {
    let obj = new Object();       // initialization

    arr.forEach((value, key) => {
        obj = value;              // assignment
    });
    return obj;                   // returns last assignment
}

Spreading ( ... ) is the key:

 let input = [{ "array1": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" }] }, { "array2": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" } ] } ]; function convert(arr) { return arr.reduce((acc, curr) => ({ ...acc, ...curr }), {}); } console.log(convert(input)) 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

You could also combine it with Object.assign on the array itself:

 let input = [{ "array1": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" }] }, { "array2": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" } ] } ]; function convert(arr) { return Object.assign(...arr); } console.log(convert(input)) 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

Using your code

function convert(arr) {
   let obj = new Object();
   arr.forEach((value, key) => {
     obj[key] = value;
   });
   return obj;
}

You were doing obj = value; on each iteration in the forEach which would mean you reassign obj to be the new value every time.

You just need to use your key parameter of foreach function in your object. I am attaching the code below you can look.

 let input = [{ "array1": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" }] }, { "array2": [{ "id": 1, "name": "Test 1" }, { "id": 2, "name": "Test 2" } ] } ]; function convert(arr) { let obj = new Object(); arr.forEach((value, key) => { obj[key] = value; }); return obj; } console.log(convert(input)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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