Converting JavaScript object with numeric keys into array

Asked
Active3 hr before
Viewed126 times

8 Answers

arrayjavascriptobject
90%

1 @NikhilAgrawal The trick here is to use Object.keys(), which returns the keys of the object (= its own enumerable properties) as an array. Then we can use array.map to replace each key by the corresponding value in a new array. – antoine Jun 19 '15 at 22:12 ,Here is an example of how you could get an array of objects and then sort the array.,Nothing hard here. Loop over your object elements and assign them to the array,If you check the polyfill, Object.assign(target, ...sources) just copies all the enumerable own properties from the source objects to a target object. If the target is an array, it will add the numerical keys to the array literal and return that target array object.

It's actually very straight forward with jQuery's $.map

var arr = $.map(obj, function(el) {
   return el
});

and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map

var arr = Object.keys(obj).map(function(k) {
   return obj[k]
});

In ES2015 there's Object.values to the rescue, which makes this a breeze

var arr = Object.values(obj);
load more v
88%

I want to convert it into a JavaScript array like this:,I have an object like this coming back as a JSON response from the server:,This works for converting json into multi-diminsional javascript arrays as well. ,Then you can turn this into a javascript array using the following

I have an object like this coming back as a JSON response from the server:

{
   "0": "1",
   "1": "2",
   "2": "3",
   "3": "4"
}

I want to convert it into a JavaScript array like this:

["1", "2", "3", "4"]
load more v
72%

Finally, with ES2017, it's official now! We have 3 variations to convert an Object to an Array 🎊, 5 Ways to Convert a Value to String in JavaScript , JavaScript Exponentiation Operator , Convert Iterable to Array using Spread in JavaScript

const zoo = {
   lion: '🦁',
   panda: '🐼',
};

Object.keys(zoo);
// ['lion', 'panda']

Object.values(zoo);
// ['🦁', '🐼']

Object.entries(zoo);
// [ ['lion', '🦁'], ['panda', '🐼'] ]
load more v
65%

To convert an object to an array you use one of three methods: Object.keys(), Object.values(), and Object.entries().,To convert property’s names of the person object to an array, you use the Object.keys() method:,To convert property’s values of the person object to an array, you use the Object.values() method:,Summary: in this tutorial, you will learn how to convert an object to an array using Object’s methods.

Suppose that you have a person object as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
const person = {
   firstName: 'John',
   lastName: 'Doe'
};
Code language: JavaScript(javascript)
load more v
75%

How to convert an Object {} to an Array [] of key-value pairs in JavaScript?,How to convert two-dimensional array into an object in JavaScript ?,How to add an object to an array in JavaScript ?,How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ?

Object.keys(obj)
load more v
40%

I have an object like this coming back as a JSON response from the server:,I want to convert it into a JavaScript array like this:,and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map,That's assuming it's already parsed as a javascript object, and isn't actually JSON, which is a string format, in that case a run through JSON.parse would be necessary as well.

I have an object like this coming back as a JSON response from the server:

{
   "0": "1",
   "1": "2",
   "2": "3",
   "3": "4"
}

I want to convert it into a JavaScript array like this:

["1", "2", "3", "4"]
load more v
22%

const names = ['Alex', 'Bob', 'Johny', 'Atta'];

// convert array to th object
const obj = Object.assign({}, names);

// print object
console.log(obj);

// {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
load more v
60%

Method 2: Object.values(),Method 1: Object.keys(),Let's look at some more different coding examples to understand more about the conversion of javascript objects to arrays. Consider the snippets below.,To convert an object into an array in Javascript, you can use different types of methods. Some of the methods are Object.keys(), Object.values(),and Object.entries().

The Lion
load more v

Other "array-javascript" queries related to "Converting JavaScript object with numeric keys into array"