1

I have two arrays, one is a list of names, and one is a list of age that is generated with a function. To make things easier, i'll just show the final result of the two arrays.

let nameList=["John","Brenda","Sabrina","Ray","Jacob"];
let ageList=[ 23, 29, 30, 23, 25 ]

I wanted to create a new array where i can combine each name paired with the age number of the same sequence. Ex: John will have 23 next to it, Brenda 29, Sabrina 30, and so on.

I have written this function to do it

function combineData (nameArray,ageArray,combArray){
    let tempComb=[];
    for (let i=0;i<nameArray.length;i++) {
        tempComb.push(nameArray[i]);
        tempComb.push(ageArray[i]);
        combArray.push(tempComb);
    }
}

The result of this loop kinda did what i wanted to get, but not really. Below is the result it returns

[ [ 'John', 23, 'Brenda', 29, 'Sabrina', 30, 'Ray', 23, 'Jacob', 25 ],
  [ 'John', 23, 'Brenda', 29, 'Sabrina', 30, 'Ray', 23, 'Jacob', 25 ],
  [ 'John', 23, 'Brenda', 29, 'Sabrina', 30, 'Ray', 23, 'Jacob', 25 ],
  [ 'John', 23, 'Brenda', 29, 'Sabrina', 30, 'Ray', 23, 'Jacob', 25 ],
  [ 'John', 23, 'Brenda', 29, 'Sabrina', 30, 'Ray', 23, 'Jacob', 25 ] ]

can anybody tell me how to prevent it from duplicating?

Extra question. Is there a way to put each name & age pairing into their own array and then combine it with the others which will create a nested array like this?

[['John', 23],['Brenda',29],['Sabrina',30],['Ray',23],['Jacob',25]]

5
  • I forgot to add that i added a third variable of let combList=[]; Commented Jul 17, 2022 at 13:41
  • And this is the final function being called combineData(nameList,ageList,combList); Commented Jul 17, 2022 at 13:42
  • Declare temp inside the loop so it becomes independent Commented Jul 17, 2022 at 13:50
  • @huseyintugrulbuyukisik holy crap this is it..thanks a lot! Okay, so i'm fairly new to javascript and i'm trying to understand why i need to put it inside. Is it because so that the temp will be empty again before i push it into the combArray after the previous iteration? Commented Jul 17, 2022 at 13:56
  • Javascript looks like its made of pointers to objects so each time you add actually adds pointer to same object. But in a loop it re-creates new object and they are different pointers. Commented Jul 17, 2022 at 13:59

3 Answers 3

1

You can map over the names and use the index to add the element from the ages array.

const nameList = [ 'John', 'Brenda', 'Sabrina', 'Ray', 'Jacob' ];
const ageList = [ 23, 29, 30, 23, 25 ];

const out = nameList.map((name, i) => {
  return [name, ageList[i]];
});

console.log(out);

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

Comments

0

This is a one liner in various underscore like libraries. The function usually called zip.

const results = _.zip(nameList, ageList)

Comments

0
const nameList = ["John", "Brenda", "Sabrina", "Ray", "Jacob"];
const ageList = [23, 29, 30, 23, 25];

function zipper(arr1, arr2) {
  const temp = {};
  for (let i = 0; i < arr1.length; i++) {
    temp[arr1[i]] = arr2[i];
  }
  return Object.entries(temp);
}

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.