I have an array
var topics = [];
I have a list of numbers 1-10
var obj = {one:1, two:2, three:3, four:4, five:5, six:6, seven:7, eight:8, nine:9, ten:10};
I just want to push the number into the array but not sure what to call it in the push function?:
$.each(obj, function(i, val) {
topics.push({
count: val
});
console.log(val);
});
If your intention is for the final array to look like this:
Then within your existing function you just need to say:
If your intention is for the final array to look like this:
Then within your existing function you just need to say:
If your intention is for the final array to look like this:
Then you would do it like this:
You can’t just say:
Because JS object literal syntax doesn’t allow use of variables for the property names (keys) though it does allow variables for the property values, so
{ i : val }creates an object with one property called “i”.In your code, if
countis a variable you would use it like I showed above, i.e.,:If “count” is the actual string that you want the property to be called then your code will work as is.