0

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.

2 Answers 2

2

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)?>};
Sign up to request clarification or add additional context in comments.

1 Comment

right.. offcourse.. sorry, i was over thinking this. staring blind! Thanks for the support.
0

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.

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.