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]]
let combList=[];combineData(nameList,ageList,combList);pushit into thecombArrayafter the previous iteration?