3

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

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've seen are things which would tack on the second array to the first (i.e. "fd.com/product1/,fd.com/product2/,d.com/product3/,image1.jpg,image2.jpg,image3.jpg") which isn't too helpful...?

Can this sort of thing be done in jQuery?

3 Answers 3

4

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);

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

2 Comments

You're right it works, I'm confused though as the docs state it's the other way around: api.jquery.com/map
@RoryMcCrossan It's not jQuery, just vanilla JavaScript.
1

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]);
}

Comments

0

You can use javascript concat() as follows -

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);

1 Comment

This is exactly the result that the OP states he doesn't want.

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.