0
var number = [1,2,3,4];
var assignment= ["first", "second", "third", "fourth"];

I need to combine these two arrays so that it console logs this information like so:

1 first
2 second
3 third
4 fourth

Thank you.

Barmar
  • 741,623
  • 53
  • 500
  • 612

4 Answers4

1

You can combine them like

var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
// alert (number[i] + " = " + assignment[number[i]-1]);
// change "i" as desired
alert (number[0] + " = " + assignment[number[0]-1]);

Now you can loop and combine them like this,

var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
for(var i=0;i<4;i++)
{
  alert (number[i] + " = " + assignment[number[i]-1]);
}

I you want a string, you can do something like this,

var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
var txt = "";
for(var i=0;i<4;i++)
{
  txt = txt + (number[i] + " = " + assignment[number[i]-1] + " ");
}
alert(txt);
lu5er
  • 3,229
  • 2
  • 29
  • 50
0

If the arrays has the same leght, it should do the work

var number = [1,2,3,4];
var assignment= ["first", "second", "third", "fourth"];
var result = new Array(number.length)
for(var i=0;i<number.length;i++){
    result[i] = (number[i] + " " + assignment[i]);
}

then you can print them

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

The following will work as long as the arrays are equal in size:

var number = [1,2,3,4];
var assignment = ["one","two","three","four"];
var size = number.count;
for(var i=0; i<size; i++){
    var log = number[i] + " " + assignment[i];
    console.log(log);
}
Billcountry
  • 585
  • 5
  • 6
0

Try forEach:

var arr = [];
var number = [1, 2, 3, 4];
var assignment = ["first", "second", "third", "fourth"];
var i = 0;
number.forEach(function(e) {
  arr.push(e + ' ' + assignment[i++])
});
console.info(arr)

WARNING: We assume the arrays size are equal

Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35