0
let objArray = [ {value:null} , {value:null}];
let dataArray= [{0:12},{1:23}];

How can I get the data from dataArray to assign to 'value' in objArray ? I want to obtain an object array like below:

objArray = [{value:12},{value:23}];

2 Answers 2

4

You can do this.

let objArray = [ {value:null} , {value:null}];
let dataArray= [{0:12},{1:23}];

const result = objArray.map((item, index) => {
  item.value = dataArray[index][index]
  return item;
})

console.log(result);

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

1 Comment

If you want to keep the result in objArray, please assign the result simply objArray = objArray.map...., not using the result const.
1

it is pretty simple. You can use assign property values of dataArray to objArray.

objArray[0].value = dataArray[0][0]; //Note: I used bracket notation instead of dot (.) notation for accessing objects in dataArray
objArray[1].value = dataArray[1][1];

You can also iterate (loop) through arrays to assign values, but let it be practice for yourself to learn.

Good luck!

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.