8

What is the easiest way to insert a new object into an array of objects at the index position 0? No jQuery; MooTools okay; no unshift() because it's undefined in IE according to w3schools.

6
  • You'll probably still get responses saying "have you tried jquery?". :) Commented Feb 4, 2010 at 5:13
  • 1
    @JamesBrownIsDead, Have you tried jQuery? You really should. :P :P Commented Feb 4, 2010 at 5:18
  • 3
    @CMS is correct: unshift works on IE: jsbin.com/umolo Commented Feb 4, 2010 at 5:19
  • Yes, works from IE5 up, it even worked on Netscape Navigator 4 (as of 1999!!) Commented Feb 4, 2010 at 5:21
  • @o.k.w: I'll choose MooTools over jQuery every single day. Commented Feb 4, 2010 at 5:42

4 Answers 4

28

W3CSchools is really outdated, the unshift method is part of the ECMAScript 3rd Edition standard which was approved and published as of December 1999.

There is no reason to avoid it nowadays, it is supported from IE 5.5 up.

It is safe to use it, even modern libraries like jQuery or MooTools internally use it (you can check the source code :).

var array = [2,3];
array.unshift(1); // returns 3
// modifies array to [1, 2, 3]

This method returns the new array length, and inserts the element passed as argument at the first position of the array.

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

3 Comments

The questioner specifies no unshift because w3schools says it is unsupported in IE. As @CMS points out, that is out-of-date: w3schools doesn't even track IE<6 market share any more.
@CMS: I'd un-down-vote you after your clarification, but because you did it in the 5min grace window & therefore generated no revision history, it says I can't until you edit your answer (even though you did)... Sorry.
@CMS: almost edited your response myself to let me change my vote, but I figured that was a bit rude. :) Anyway, I've switched it to an up-vote now. Well deserved.
6

Using splice method.

array.splice(index,howmany,element1,.....,elementX);

For inserting, 'howmany' = 0

2 Comments

unshift and concat are simpler than splice for what @JamesBrownIsDead wants to do.
@Dominic, very true indeed. I guess I'm just 'lazy' to remember different methods and just the more generic ones :P
5

You can try this:

var newItem = 3;
arr = [newItem].concat(arr);

Another option is to use push and reverse the indices - you can easily write an object that does that.

Comments

0

I would use unshift() if your intent is to modify the array, otherwise you can use concat() as Kobi suggested (which returns a new array and does not modify the involved arrays):

var arr = [1, 2], newItem = 3;
var newArr = [newItem].concat(arr); // newArr = [1, 2, 3], arr still = [1, 2]

See: concat

Comments

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.