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

2

3 Answers 3

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 );
Sign up to request clarification or add additional context in comments.

4 Comments

So, the updated final array can be got by myObj['fruit'] = [].push.apply( myObj["fruit"], toAppend ); I guess.
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...
mplungjan's proposal is also great: stackoverflow.com/questions/5240335/…
@Leem - That suggests .push.apply() just like the second section of my answer :)
1

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

1 Comment

Better reference: MDC
-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]);
}

:)

5 Comments

Looping arrays with for ... in is a bad idea... Better to iterate for (var i=0, l=array.length, i<l, i++)
can you expand your opinion about this for ... in? i cant see any problem with it
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: jsfiddle.net/gnarf/YjVQY --- Because of this you should NEVER use for ... in on arrays, considering they have lengths and numeric properties...
ah ok, that is applicable if we add a new prototype... well, maybe i should start to use your way. nice sharing! :)
Also, FYI - generally, any uses of a for ... in should also use .hasOwnProperty()

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.