Uncaught TypeError: data.push is not a function
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 = [{}, {}, {}];
Javascript push is not a function
It means that your storeArray[storename]
is not an array.
At
storeArray[storename] = response.data.bookInfo[i];
you just assign a value to the storeArray[storename]
which is not an array. You need first to create an array and then put that element into it.
storeArray[storename] = [ response.data.bookInfo[i] ];
and at the next iterations you will have an array with one element and can use push
on it.
Array.push is not a function with return Array
As others have pointed out, you're replacing the collections
array with a single object containing the updated element for the given character. You should just update that element in place rather than reassign the array itself by replacing that assignment with:
collections[i].number++;
Your function can also be simplified by replacing the loop with the find()
method.
checkuniqueness(collection, character) {
let counter = collection.find(o => o.character == character);
if (counter) {
counter.number++;
} else {
collection.push({
character: character,
number: 1
});
}
return collection;
}
Push is not a function JavaScript error
This will create your example.
var myArray = new Array(4);
for (var i = 0; i < myArray.length; i++) {
myArray[i] = ["ID", "SomeValue" + (i+1)];
}
But if you need to set data from a database, how is that being set in the Javascript? if it's in a different array you could do the following:
var dbArray = ["SomeValue1", "SomeValue2", "SomeValue3"];
var myArray = new Array(dbArray.length);
for (var i = 0; i < myArray.length; i++) {
myArray[i] = ["ID", dbArray[i]];
}
Javascript: .push is not a function
Array.push
doesn't return an array. It returns the new length of the array it was called on.
So, your return array.push(b);
returns an int
. That int gets passed back as array
... which is not an array so it doesn't have a .push()
method.
You need to do:
array.push(b);
return array;
I am getting error push() is not a function while pushing element in array?
You should delete the [i] in abc[i].push()
because abc[i]
is string, but the .push()
method works for arrays. Therefore you should use abc.push()
instead of abc[i].push()
.
Related Topics
Javascript Function Not Defined Error (But It Is Defined)
React Js Error: Is Not Defined React/Jsx-No-Undef
Javascript - Auto Click on a Button on Page Load
Javascript Check If Values Exist in Array Json
How to Update a Variable After Button Click
How to Disable Clicking Inside Div
How to Hide the Parent Div If All Its Child Div Are Hidden
Html Input File Selection Event Not Firing Upon Selecting the Same File
How to Clear the Focus in JavaScript
Regex to Replace Everything Except Numbers and a Decimal Point
How to Automatic Click a Button on a Webpage
How to Have Multiple Buttons of Same Id Value and When Click on Any Button the Pop-Up Should Come
How Could I Get the Latest Date from an Array of Dates - Javascript/Typescript
Why React Props Are Passed Undefined to the Child Component
Correct Way to Push into State Array
How to Disable Onclick Without Altering Its Function
Javascript Regex - No White Space At Beginning + Allow Space in the Middle