Reputation: 1218
I have the following issue, I'm trying to merge an array into another array with a specific key. For example:
var params = new Array();
params.push({"category":<?php json_encode($category)?>});
So far so good. params is now a filled array with some key called "category".
But when i want to call that array, it says params.category undefined!
I can call it like params[0].category.
How can i remove the leading 0 key?
I have tried concat but with the same result.
Upvotes: 0
Views: 48
Reputation: 1879
It says undefined because you are pushing an object to the array which has one element (of type object). Since it is an array, you have to provide the array index to access the value.
If you want to access using .[dot] operator only, use an object like
var params = {};
params["category"] = <?php json_encode($category)?>;
or
var params = {"category": ?php json_encode($category)?>};
Courtesy ChrisC.
Upvotes: 0
Reputation: 4446
If you don't want the [0] then you don't want to index into an array.
So just use params as an object:
var params = {};
params["category"] = <?php json_encode($category)?>;
or
var params = {"category": ?php json_encode($category)?>};
Upvotes: 2