-2

I need to convert two arrays of strings with the same length:

const arr1 = ['Jessica', 'Ben', 'Samantha', 'John', 'Sandy'];
const arr2 = ['21', '45', '34', '90', '67']; 

And in the end i need to get this array with particular keys name, age:

const result = [
{ name: 'Jessica', age: '21'}, 
{ name: 'Ben', age: '45'}, 
{ name: 'Samantha', age: '34'},
{ name: 'John', age: '90' },
{ name: 'Sandy', age: '67' },
];

Can you tell me please how can i do it?

7
  • of course! and i did not find a good solution, help me if you know it Commented Feb 27, 2020 at 9:43
  • Please post your attempt for solving this, and if you face any problem with it, we can help you. Commented Feb 27, 2020 at 9:45
  • @jocoders Then please share your attempt in question. SO is not a get code for free site Commented Feb 27, 2020 at 9:47
  • but it is not the same question that you have posted! Commented Feb 27, 2020 at 9:49
  • 1
    @jocoders, Here you can get the result you want codepen.io/Maniraj_Murugan/pen/KKpmKPM Commented Feb 27, 2020 at 9:54

1 Answer 1

5

Loop through the arr1. You get the names and the index. Based on index, get the ages from arr2 considering name and age array indexes are same

const arr1 = ['Jessica', 'Ben', 'Samantha', 'John', 'Sandy'];
const arr2 = ['21', '45', '34', '90', '67']; 

const result = arr1.map((name, index) => {
  return {
    'name': name,
    'age': arr2[index]
  }
})

console.log(result)

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

6 Comments

First, missing explanation. Second, please do not spoonfeed answers.
Add an explanation arr1.map((name, i) => ({name, age: arr2[i]})); and you can get upvotes.
Downvote does not mean incorrect. It means not required. Also vote is for missing explanation. Just putting code is not answer. Please explain what and why you have done
@adiga how did you do??
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.