4

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.

0

4 Answers 4

2

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

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

Comments

2

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
}

Comments

0

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.

Comments

0

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

2 Comments

Some IDEs will object to your new Object, instead use let obj = {};
I believe it is identical to stackoverflow.com/a/55530612/295783

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.