1

I'm trying to produce a new_array consisting of old_array[0] + old_array2.slice(1)

I cannot see how concat would help me here, unless I create a 3rd temporary array out of old_array[0].

Is that the only way to do it?

0

3 Answers 3

1
var a = [1,2,3];

var b = [4,5,6];

Then you have two options depending on the result you want. Either:

var c = a.slice(1).concat(b[0]);

Or:

var c = [b[0]].concat(a.slice(1));
Sign up to request clarification or add additional context in comments.

Comments

1

The function you are looking for is array.unshift(), which adds a value (or values) to the start of the array:

var fruits = [ "orange", "banana" ];
fruits.unshift("apple");
// Fruits is now ["apple", "orange", "banana"]

If you want to produce a new array, you could do this to the result of a slice:

var new_array = old_array2.slice(1);
new_array.unshift(old_array[0]); 

But, since this code just replaces the first element, you could also write:

var new_array = old_array2.slice(0);
new_array[0] = old_array[0]; // I prefer this for readability

I'm not sure about the slice(1) in your question, as I don't think it matches the wording of your question. So, here are two more answers:

  • If you want to create a new array which is the same as old_array2, but with the first value from old_array at the start of it, use:

     var new_array = old_array2.slice(0); // Note the slice(0)
     new_array.unshift(old_array[0]);
    
  • If you want to create a new array which is the same as old_array2, but with the first value replaced with the first value of old_array, use:

    var new_array = old_array2.slice(0);
    new_array[0] = old_array[0]; 
    

Comments

0

If you simply want a new two-element array from those explicitly defined portions of two source arrays, then just do this:

new_array = [old_array[0], old_array2.slice(1).toString()];

See: jsfiddle

(Note: Second element fixed from originally posed solution, per comment received.)

5 Comments

This will result in a two element array, with the second element being an array, which is not what the poster is asking for.
Looking at your JS fiddle, the alert(array.toString()) is misleading, because it doesn't show array boundaries. Look at the result in a javascript console - you'll see it's still a two element array, which is not what the poster wants.
Thanks, @Timothy Jones. I've updated it. I think the poster just wants a new two-element array of the two strings; the item a [0] in old_array and the sliced item in the second array.
I don't see that from his question, but I agree that there's not a lot of information in it. I will hold off from downvoting you in case your interpretation is correct- but perhaps you could add a note to the answer that makes it clear that your code produces a two element array?
Thanks for the non-down-vote :-) When I read the question I thought, "He's not asking for a single element array from two strings, is he?" so I wasn't super sure myself. I mean, generally when somebody want a new array, it's more than one element, since making one from a string is so easy. Trick questions perhaps? :-)

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.