0

I want to join two objects that are inside an array:

const test = [{name: ""}, {age: ""}, {nac: ""}];

I need it to look like this:

const test2 = [{name: "", age: "", nac: ""}];

I tried with: Object.assign () but I can't get it.

Any other ideas I can try?

2 Answers 2

3

You can use Object.assign with spread syntax.

const test = [{name: ""}, {age: ""}, {nac: ""}];
const test2 = [Object.assign({}, ...test)];
console.log(test2);

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

Comments

0

You can use array.prototype.reduce function that will be useful when reducing an array to a single value. I have given example below

const test = [{name: ""}, {age: ""}, {nac: ""}];
const reducer = (accumulator, currentValue) => {
  for (var key in currentValue) {
    accumulator[key] = currentValue[key];
  }
  return accumulator;
};

var finalObj = test.reduce(reducer, {});
console.log(finalObj);
console.log([finalObj]);

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.