0

I got a response from a REST call, which returns and array with objects

response.data.steps

For example it looks like this

enter image description here

Now I need to add to each Child of this array an new Object Array.

What could be a smart solution for this problem?

Thanks.

3
  • You want to make a copy of the returning array? Commented Sep 5, 2017 at 8:34
  • 1
    Do you want to return each item as an array or add a new array property to each item? Commented Sep 5, 2017 at 8:40
  • adding a new array to each item Commented Sep 5, 2017 at 8:41

2 Answers 2

2

In order to add a new array property to each item, you can simply do:

const steps = response.data.steps.map(step => ({
  ...step,
  newObjectArray: [],
}))
Sign up to request clarification or add additional context in comments.

Comments

1

you can usee Array.prototype.map() to do so

let result = response.data.steps.map(element => {
    let ret = [element];
    return ret;
});

let arr = [{a:1}, {a:2}, {a:3}];

arr = arr.map(element => {
    let ret = [element];
    return ret;
});

console.log(arr);

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.