Concatenating elements of two jQuery string arrays

I have two arrays of strings called old_array and new_array and I want to concatenate them together like this:

old_array = "fd.com/product1/,fd.com/product2/,fd.com/product3/"

new_array = "image1.jpg,image2.jpg,image3.jpg"

(code happens in this area)

final_array = "http://www.fd.com/product1/image1.jpg,http://www.fd.com/product2/image2.jpg,http://www.fd.com/product3/image3.jpg"

      

All I have seen are things that will touch the second array to the first (eg "fd.com/product1/,fd.com/product2/,d.com/product3/image1.jpg" image2. Jpg, image3. jpg "), which isn't very useful ...?

Can this be done in jQuery?

+3


source to share


3 answers




var oldArray = 'fd.com/product1/,fd.com/product2/,fd.com/product3/'.split(','),
    newArray = 'image1.jpg,image2.jpg,image3.jpg'.split(',');

var finalArray = oldArray.map(function(e, i) {
        return 'http://' + e + newArray[i];
    });

document.write(finalArray);
      

Run codeHide result


+4


source


You can do something like this without using jQuery ...



final_array= [];
for (var i=0,j=old_array.length; i<j; i++) {
    final_array.push('http://' + old_array[i] + new_array[i]);
}

      

+1


source


You can use javascript concat () like this:

var old_array = ["fd.com/product1/","fd.com/product2/","fd.com/product3/"];
var new_array = ["image1.jpg","image2.jpg","image3.jpg"];
var final_array = old_array.concat(new_array);
alert (final_array);

      

0


source







All Articles