Reputation: 1869
I have an object like this:
{"Peppermint":"50","Chocolate":"50"}
I want to turn it into this:
[['Peppermint', '50']['Chocolate', '50']]
Using the jquery map function like so:
var array = $.map(data, function(value, index) {
return [value];
});
Gives me this without the keys:
["50", "50"]
Upvotes: 0
Views: 31
Reputation: 1
Try returning array in array from $.map()
with index
at index 0
, value
at index 1
of inner array
var data = {"Peppermint":"50","Chocolate":"50"};
var array = $.map(data, function(value, index) {
return [[index, value]]
});
console.log(array)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 707228
You have to return the inner array with both values in it and that has to be embedded in another array. You need this extra level of array because from the jQuery doc for $.map
:
A returned array will be flattened into the resulting array.
So, you need code like this working snippet:
var data = {"Peppermint":"50","Chocolate":"50"};
var array = $.map(data, function(prop, key) {
return [[key, prop]];
});
document.write(JSON.stringify(array));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Upvotes: 1