0

So to explain this, I am creating a JSON object and with this object, I would like to be able to modify it like a PHP array. Meaning I can add more values into the array at any given time to a key.

For instance, PHP would be like this:

$array = array();
$array['car'][] = 'blue';
$array['car'][] = 'green';
$array['car'][] = 'purple';

You can see that PHP can add more data into the array object with the key of 'car'. I'm wanting to do that same thing for a JSON object except it may not always be as a string for the key.

function count(JSONObject) {
    return JSONObject.length;
}

test = {};
test[100] = {
  charge: "O",
  mannum: "5",
  canUse: "Y"
};

I know you can create new objects like this, but this is not what I'm wanting to do.

test[101] = {
  charge: "O",
  mannum: "5",
  canUse: "Y"
};

This is what I can think of but I know it doesn't work:

test[100][count(test[100])] { // Just a process to explain what my brain was thinking.
  charge: "N",
  mannum: "7",
  canUse: "N"
}

I'm expecting the results to be somewhat like this (It also doesn't have to look exactly like this):

test[100][0] = {
  charge: "O",
  mannum: "5",
  canUse: "Y"
};
test[100][1] { 
  charge: "N",
  mannum: "7",
  canUse: "N"
}

How can I go about this so I can add more data into the object? I appreciate everyone's input for helping me find a resolution or even some knowledge.

7
  • test.push( {charge: 'N', mannum : '7', canUse : 'N'} ) Commented Nov 26, 2014 at 16:14
  • Whops, I had to think about that for a minute. I fixed it to explain what I mean, correctly. @adeneo wouldn't that only push to a new key? Example to 101, and not to 100? Or do you mean test[100].push() ? Commented Nov 26, 2014 at 16:19
  • I'm not sure what you mean, javascript != PHP, there are no associative arrays in javascript, and you can't think the same you're doing in other languages, you'll have to adapt, not try to adapt the code to look like another language. Commented Nov 26, 2014 at 16:23
  • @adeneo Thats why I posted an example, to explain what I'm trying to figure out. I know Javascript is NOT PHP. Commented Nov 26, 2014 at 16:25
  • 1
    OK, then test[100] has to be an array and you simply call .push. Commented Nov 26, 2014 at 16:38

2 Answers 2

2

It seems like this is what you want:

test = {};
test[100] = [{ // test[100] is an array with a single element (an object)
  charge: "O",
  mannum: "5",
  canUse: "Y"
}];

// add another object
test[100].push({
  charge: "N",
  mannum: "7",
  canUse: "N"
});

Learn more about arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand well, you're trying to convert this into javascript:

PHP

$array = array();
$array['car'][] = 'blue';
$array['car'][] = 'green';
$array['car'][] = 'purple';

JAVASCRIPT

var array = {};
array['car'] = ['blue', 'green', 'purple'];

EXPLANATION

PHP associative arrays -> {} in JSON

PHP indexed arrays -> [] in JSON

UPDATE1

I'm expecting the results to be somewhat like this (It also doesn't have to look exactly like this):

test[100][0] = {
  charge: "O",
  mannum: "5",
  canUse: "Y"
};
test[100][1] { 
  charge: "N",
  mannum: "7",
  canUse: "N"
}

Try this:

var test = {};
test[100] = [{"charge": "O", "mannum": "5", "canUse": "Y"}, {"charge": "N", "mannum": "7", "canUse": "N"}];

3 Comments

I'm not trying to convert the PHP array into Javascript. Plus this would be better answered by json_encode($array);. ^_~.
Don't confuse object literals in JavaScript with JSON. You didn't post any JSON at all.
Thank you for contributing! However, I will not have the data on me at any given time, since the information will come from a database. So I need a dynamic approach.

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.