Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Object to Array - maintain named keys

I am converting an object into an array. The keys of the object contain names such as "cool", as can be seen below:

var obj = {
  "cool": "Mustang",
  "family": "Station Wagon",
  "small": {
    0: "small car 1",
    1: "small car 2"
  }
};

When converting, the returned array looks as followed:

Array[3]
0:"Mustang"
1:"Station Wagon"
2:Object
    0:"small car 1" 
    1:"small car 2"

As you can see in the array above the names of keys from the object such as "cool" are lost, instead they are replaced with numbers. Furthermore, the array contains an object, I would like this object to be an array within the array.

I would like the returned array to be like this instead:

Array[3]
"cool":"Mustang"
"family":"Station Wagon"
"small": Array[2]
    0:"small car 1" 
    1:"small car 2"

I would very much appreciate your help. A fiddle with the code can be found here: https://jsfiddle.net/v02q4sy2/8/

var obj = {"cool":"Mustang","family":"Station Wagon","small":{0:"small car 1",1:"small car 2"}}

var arr = $.map(obj, function(value, index) {
    return [value];
});

console.log(arr);
like image 568
user3398797 Avatar asked Feb 03 '17 02:02

user3398797


People also ask

How to convert an object to an array[] of key-value pairs?

We can convert an Object {} to an Array [] of key-value pairs using methods discussed below: Method 1: In this method, we will use Object.keys () and map () to achieve this. Approach: By using Object.keys (), we are extracting keys from the Object then this key passed to map () function which maps the key and corresponding value as an array, ...

How to convert string-keyed properties of an object to array?

To convert the enumerable string-keyed properties of an object to an array, you use the Object.entries () method. For example: const entries = Object .entries (person); console .log (entries); Code language: JavaScript (javascript)

How to convert the values of an object to an array?

To convert property’s values of the person object to an array, you use the Object.values () method: To convert the enumerable string-keyed properties of an object to an array, you use the Object.entries () method. For example: Was this tutorial helpful ?

How to convert a person object’s name to an array?

To convert property’s names of the person object to an array, you use the Object.keys () method: const propertyNames = Object.keys (person); console.log (propertyNames); Code language: JavaScript (javascript)


1 Answers

Based on the code that you provided, it looks like you would just need to return an object within the map method containing the corresponding key/value pair:

Updated Example

var arr = $.map(obj, function(value, key) {
  return { [key]: value };
});

You don't need jQuery for this though. Here is a simple example with plain JavaScript utilizing the native .map() method along with Object.keys() to retrieve a mappable array of keys from the object:

Updated Example

var arr = Object.keys(obj).map(function (key) {
  return { [key]: obj[key] };
});
like image 50
Josh Crozier Avatar answered Sep 21 '22 05:09

Josh Crozier