2

I have an array in javascript

var myArr = {
 'textId':'123', 
 'type':'animal', 
 'content':'hi dog', 
 'expires':'27/10/2012' 
};
$.each(myArr, function(myArrArrKey, myArrArrValue){
 console.log( myArrArrValue );
});

The above console prints following values

123
app
hi app
27/10/2012

Now i am want to append an element to the existing array, i am trying to do like the below one

myArrValue.push({'status':'active'});

The above push throws following error

TypeError: Object #<Object> has no method 'push'

Kindly help me how to append to that existing array element.
I want to print the array like

123
app
hi app
27/10/2012
active
Andreas Louv
47.3k14 gold badges109 silver badges126 bronze badges
asked Oct 27, 2012 at 11:15
2
  • That is an object, not an array. More importantly, your output does not seem to match your input in the first case. Commented Oct 27, 2012 at 11:17
  • this is a json object not an array Commented Oct 27, 2012 at 11:18

7 Answers 7

4

Just do this.

myArr.status = 'active'

or

myArr["status"] = 'active'

Your myArr is an Object not an Array..

push function is available to an Array variable.

answered Oct 27, 2012 at 11:17
Sign up to request clarification or add additional context in comments.

Comments

3

this isn't an array, it's an object!

var myArr = {
 'textId':'123', 
 'type':'animal', 
 'content':'hi dog', 
 'expires':'27/10/2012' 
};

this isn't necessary with jQuery

$.each(myArr, function(myArrArrKey, myArrArrValue){
 console.log( myArrArrValue );
});


easier would be

for ( var k in myArr ) {
 console.log( myArr[ k ];
}


to add new entries to your "array"

myArr[ 'foo' ] = 'bar'; // this is array notation

or

myArr.foo = 'bar'; // this is object notation


to remove entries from your "array"

delete myArr[ 'foo' ];

or

delete myArr.foo;


FYI: myArrValue.push({'status':'active'}); wont work. myArrValue isn't the "array" itself and also not an array having the method push. If it would be an array the result would be, that your latest entry is the whole an object {'status':'active'}

answered Oct 27, 2012 at 11:25

Comments

2

The answer is in the error... you have an object, not an array. Use object notation

myArr.status='active'
answered Oct 27, 2012 at 11:18

Comments

2

Simply use:

myArrValue.status = 'active';

but be aware that what you are using is an object, not an array. Another way to add properties to an object is:

object[key] = value;
answered Oct 27, 2012 at 11:19

Comments

2

this a json object not array push would for an array for json you do

myObj.NewProp = 123;
answered Oct 27, 2012 at 11:19

Comments

2

Just for the kicks of it..

function push( obj ) {
 var prop;
 for ( prop in obj ) {
 this[prop] = obj[prop];
 }
 return this;
}

Your object, remember to assing the push method.

var obj = {
 a: "a",
 b: "b",
 push: push
};

And the push:

obj.push({
 c: "c",
 d: "d"
});
answered Oct 27, 2012 at 11:22

Comments

1
myArr["status"] = 'active';

or

myArr.status ='active';
answered Oct 27, 2012 at 11:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.