48

I was looking for a way to add new elements to an an existing object like what push does with arrays

I have tried this and it didn't work :

var myFunction = {
    Author: 'my name ',
    date: '15-12-2012',
    doSomething: function(){
        alert("helloworld")
    }
};
myFunction.push({
    bookName:'mybook',
    bookdesc: 'new'
});
console.log(myFunction);
2
  • 4
    myFunction is an object. Objects don't have a push method. (neither do functions) Commented Jun 15, 2012 at 20:35
  • I know this is old, but it seems at least a little confusing to name a variable myFunction. Commented May 26, 2021 at 15:09

6 Answers 6

75

Use this:

myFunction.bookName = 'mybook';
myFunction.bookdesc = 'new';

Or, if you are using jQuery:

$(myFunction).extend({
    bookName:'mybook',
    bookdesc: 'new'
});

The push method is wrong because it belongs to the Array.prototype object.

To create a named object, try this:

var myObj = function(){
    this.property = 'foo';
    this.bar = function(){
    }
}
myObj.prototype.objProp = true;
var newObj = new myObj();
Sign up to request clarification or add additional context in comments.

3 Comments

thanks that sounds good , but my other question about how can we give a name to the object
What name? Could you explain better?
when you do a console.log(yourObject) it comes up as named object how can we change that display name ?
15

Just do myFunction.foo = "bar" and it will add it. myFunction is the name of the object in this case.

1 Comment

works for me. marked as correct.
7

jQuery syntax mentioned above by Danilo Valente is not working. It should be as following-

$.extend(myFunction,{
    bookName:'mybook',
    bookdesc: 'new'
});

Comments

4

You can use Extend to add new objects to an existing one.

Comments

3

You are looking for the jQuery extend method. This will allow you to add other members to your already created JS object.

1 Comment

This is also available in lodash: lodash.com/docs#isEmpty : _.isEmpty({ 'a': 1 }); // → false
3

You could store your JSON inside of an array and then insert the JSON data into the array with push

Check this out https://jsfiddle.net/cx2rk40e/2/

$(document).ready(function(){

  // using jQuery just to load function but will work without library.
  $( "button" ).on( "click", go );

  // Array of JSON we will append too.
  var jsonTest = [{
    "colour": "blue",
    "link": "http1"
  }]

  // Appends JSON to array with push. Then displays the data in alert.
  function go() {    
      jsonTest.push({"colour":"red", "link":"http2"});
      alert(JSON.stringify(jsonTest));    
    }

}); 

Result of JSON.stringify(jsonTest)

[{"colour":"blue","link":"http1"},{"colour":"red","link":"http2"}]

This answer maybe useful to users who wish to emulate a similar result.

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.