0

Array which I want change

I have got this array in TypeScript, I want it change as bellow.

list = [{id : 1, name : bus, active : 1}, {id : 2, name : car}]

Can anyone help me?

1
  • Your input is not a valid js array as one of the nested objects doesn't have a key. Commented Feb 7, 2021 at 14:34

1 Answer 1

1

You can map the input and reduce each element to an object.

const list = [
  [{ id: 1 }, { name: 'bus' }, { active: 1 }],
  [{ id: 2 }, { name: 'car' }],
];

const result = list.map((el) => {
  return el.reduce((acc, curr) => {
    return { ...acc, ...curr };
  }, {});
});

console.log(result);

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

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.