0

If I have an object defined as:

var myObj={};

Then, I update this object with:

myObj['fruit']=['apple', 'orange'];

Later, I would like to append "[banana, melon]" to myObj['fruit'], that's update myObj to

['apple','orange','banana','melon']

what is the most elegant way to update 'fruit' attribute value of myObj in my case? That's update array by appending a new array.

--------EDIT-------

I need a way to append array as one variable, not extract each element of the appended array and push. e.g. oldArray append with newArray = final array

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leem
  • 17,220
  • 36
  • 109
  • 159
  • 1
    possible duplicate of [Newbie Javascript : appending to array](http://stackoverflow.com/questions/351409/newbie-javascript-appending-to-array) – mplungjan Jun 03 '11 at 07:40
  • Very elegant: Use Apply: http://stackoverflow.com/questions/5240335/add-two-arrays-without-using-the-concat-method – mplungjan Jun 03 '11 at 07:54

3 Answers3

1

JavaScript has a built in Array.push()

myObj["fruit"].push( 'banana', 'melon' );

There are a few ways to approach appending an array. First up, use apply() to call push with the array as individual arguments:

var toAppend = ['banana', 'melon'];
// note [].push is just getting the "push" function from an empty array
// the first argument to "apply" is the array you are pushing too, the
// second is the array containing items to append
[].push.apply( myObj["fruit"], toAppend );

Also, you could concat() the arrays, however concat doesn't modify the original array so if you have other references they might get lost:

myObj["fruit"] = myObj["fruit"].concat( toAppend );
gnarf
  • 105,192
  • 25
  • 127
  • 161
  • So, the updated final array can be got by myObj['fruit'] = [].push.apply( myObj["fruit"], toAppend ); I guess. – Leem Jun 03 '11 at 08:00
  • If you are using push, it returns the new length of the array or the # of items added... `push()` affects the array it is pushing to, so you don't need to assign it to anything... – gnarf Jun 03 '11 at 08:07
  • mplungjan's proposal is also great: http://stackoverflow.com/questions/5240335/add-two-arrays-without-using-the-concat-method – Leem Jun 03 '11 at 08:10
  • @Leem - That suggests `.push.apply()` just like the second section of my answer :) – gnarf Jun 03 '11 at 08:41
1

If you don't want to push, then concat :)

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
  • 2
    Better reference: [MDC](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/concat) – mplungjan Jun 03 '11 at 07:52
-1

I would suggest to iterate the array items that you want to push, by doing this:

var newObj = ["banana", "melon"];

for (var i in newObj)
{
   myObj['fruit'].push(newObj[i]);
}

:)

kororo
  • 1,972
  • 15
  • 26
  • 1
    Looping arrays with `for ... in` is a bad idea... Better to iterate `for (var i=0, l=array.length, i – gnarf Jun 03 '11 at 07:51
  • can you expand your opinion about this for ... in? i cant see any problem with it – kororo Jun 03 '11 at 08:00
  • Properties added to the `Array.prototype` will show up when you `for ... in` -- Although altering these prototypes isn't the best idea, it is done do do things like filling in `.indexOf()` or other newer array functions that don't exist across all browsers... See this example: http://jsfiddle.net/gnarf/YjVQY/ --- Because of this you should **NEVER** use `for ... in` on arrays, considering they have lengths and numeric properties... – gnarf Jun 03 '11 at 08:39
  • ah ok, that is applicable if we add a new prototype... well, maybe i should start to use your way. nice sharing! :) – kororo Jun 03 '11 at 08:43
  • Also, FYI - generally, any uses of a `for ... in` should also use [`.hasOwnProperty()`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) – gnarf Jun 03 '11 at 08:45