[javascript] How to convert an Object {} to an Array [] of key-value pairs in JavaScript

Use Object.keys and Array#map methods.

_x000D_
_x000D_
var obj = {_x000D_
  "1": 5,_x000D_
  "2": 7,_x000D_
  "3": 0,_x000D_
  "4": 0,_x000D_
  "5": 0,_x000D_
  "6": 0,_x000D_
  "7": 0,_x000D_
  "8": 0,_x000D_
  "9": 0,_x000D_
  "10": 0,_x000D_
  "11": 0,_x000D_
  "12": 0_x000D_
};_x000D_
// get all object property names_x000D_
var res = Object.keys(obj)_x000D_
  // iterate over them and generate the array_x000D_
  .map(function(k) {_x000D_
    // generate the array element _x000D_
    return [+k, obj[k]];_x000D_
  });_x000D_
_x000D_
console.log(res);
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to type-conversion

How can I convert a char to int in Java? pandas dataframe convert column type to string or categorical How to convert an Object {} to an Array [] of key-value pairs in JavaScript convert string to number node.js Ruby: How to convert a string to boolean Convert bytes to int? Convert dataframe column to 1 or 0 for "true"/"false" values and assign to dataframe SQL Server: Error converting data type nvarchar to numeric How do I convert a Python 3 byte-string variable into a regular string? Leading zeros for Int in Swift