Uncaught TypeError: data.push is not a function

JavascriptJqueryJson

Javascript Problem Overview


I am trying to push

data.push({"country": "IN"});

as new id and value to a json string. but it gives the following error

Uncaught TypeError: data.push is not a function

data{"name":"ananta","age":"15"}

Advance Thanks for your reply

Javascript Solutions


Solution 1 - Javascript

To use the push function of an Array your var needs to be an Array.

Change data{"name":"ananta","age":"15"} to following:

var data = [
    { 
        "name": "ananta",
        "age": "15",
        "country": "Atlanta"
    }
];

data.push({"name": "Tony Montana", "age": "99"});

data.push({"country": "IN"});

..

The containing Array Items will be typeof Object and you can do following:

var text = "You are " + data[0]->age + " old and come from " + data[0]->country;

Notice: Try to be consistent. In my example, one array contained object properties name and age while the other only contains country. If I iterate this with for or forEach then I can't always check for one property, because my example contains Items that changing.

Perfect would be: data.push({ "name": "Max", "age": "5", "country": "Anywhere" } );

So you can iterate and always can get the properties, even if they are empty, null or undefined.

edit

Cool stuff to know:

var array = new Array();

is similar to:

var array = [];

Also:

var object = new Object();

is similar to:

var object = {};

You also can combine them:

var objectArray = [{}, {}, {}];

Solution 2 - Javascript

Your data variable contains an object, not an array, and objects do not have the push function as the error states. To do what you need you can do this:

data.country = 'IN';

Or

data['country'] = 'IN';

Solution 3 - Javascript

Also make sure that the name of the variable is not some kind of a language keyword. For instance, the following produces the same type of error:

var history = [];
history.push("what a mess");

replacing it for:

var history123 = [];
history123.push("pray for a better language");

works as expected.

Solution 4 - Javascript

you can use push method only if the object is an array:

var data = new Array();
data.push({"country": "IN"}).

OR

data['country'] = "IN"

if it's just an object you can use

data.country = "IN";

Solution 5 - Javascript

I think you set it as

var data = []; 

but after some time you made it like:

data = 'some thing which is not an array'; 

then data.push('') will not work as it is not an array anymore.

Solution 6 - Javascript

One things to remember push work only with array[] not object{}.

If you want to add object o inside inside n like that :

a = {
   b:"c",
   D:"e",
   F: {
      g:"h",
      I:"j",
      k: {
         l:"m"
      }
   }
}

a.F.k.n = { o: "p" };
console.log(a);

Solution 7 - Javascript

Try This Code $scope.DSRListGrid.data = data; this one for source data

            for (var prop in data[0]) {
                if (data[0].hasOwnProperty(prop)) {
                    $scope.ListColumns.push(
                            {
                                "name": prop,
                                "field": prop,
                                "width": 150,
                                "headerCellClass": 'font-12'
                            }
                    );
                }
            }
            console.log($scope.ListColumns);

Solution 8 - Javascript

make sure you push into an Array only and if their is error like Uncaught TypeError: data.push is not a function** then check for type of data you can do this by consol.log(data) hope this will help

Solution 9 - Javascript

let dataArray = [{'id':1,'code':'ABC'},{'id':1,'code':'ABC'},{'id':2,'code':'ABC'}]

    let obj = {};
  dataArray.forEach(task => {        
    task.id in obj ? obj[task.employee_id].push(task):
    obj = {
      ...obj,
      [task.employee_id]: [task],
    }
  });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnanta PrasadView Question on Stackoverflow
Solution 1 - JavascriptCagatay UlubayView Answer on Stackoverflow
Solution 2 - JavascriptRory McCrossanView Answer on Stackoverflow
Solution 3 - JavascriptPaloView Answer on Stackoverflow
Solution 4 - JavascriptrvandoniView Answer on Stackoverflow
Solution 5 - JavascriptAbdusSalamView Answer on Stackoverflow
Solution 6 - JavascriptvibhuView Answer on Stackoverflow
Solution 7 - JavascriptJayaraman MView Answer on Stackoverflow
Solution 8 - JavascriptNilesh BandekarView Answer on Stackoverflow
Solution 9 - JavascriptBilal SaleemView Answer on Stackoverflow