0

I have two arrays, one with objects containing multiple attributes:

var peopleArray = [{name: 'James', lastname: 'Stanley'}, {name: 'Roger', lastname 'Moore'}];

And one array with random words:

var wordArray = ['Banana', 'Sword'];

My goal is to add each word from wordArray as a new attribute to each object in peopleArray based on index.

E.g. the word "Banana" is at index 0 in wordArray so it will be added to the object at the same index in peopleArray, which is "James".

I hope it was clear, any guidance in any way appreciated! I really don't know how to achieve this

1
  • did u write any code ? Commented Jan 7, 2018 at 18:14

2 Answers 2

1

This is really pretty simple:

const n = Math.min(peopleArray.length, wordArray.length);
for (let i = 0; i < n; i++) {
    peopleArray[i].word = wordArray[i];
}
Sign up to request clarification or add additional context in comments.

Comments

0
for (var index = 0; index < peopleArray.length; index++) {
    peopleArray[index][nameOfNewAttr] =wordArray[index]

}

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.