Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

I want to convert an object like this:

{"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}

into an array of key-value pairs like this:

[[1,5],[2,7],[3,0],[4,0]...].

How can I convert an Object to an Array of key-value pairs in JavaScript?

Solution

You can use Object.keys() and map() to do this

var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map((key) => [Number(key), obj[key]]);

console.log(result);
Answered By: Anonymous

Related Articles

  • error LNK2005: ✘✘✘ already defined in…
  • How do I keep only the first map and when the game…
  • How do I include certain conditions in SQL Count
  • Find maximal pairs in a set of tuples
  • Making Make Automatically Compile Objs from Headers…
  • What's the difference between KeyDown and KeyPress in .NET?
  • Perfomance issues with large number of elements in…
  • Backbone Collection.fetch gives me Uncaught…
  • How do I observe sub-property changes in Polymer?
  • Warning: Undefined array key PHP when checking for a…
  • How can I access and process nested objects, arrays or JSON?
  • Wrong response from controller JSP
  • npm install error in vue
  • Convert JavaScript string in dot notation into an…
  • How does the "this" keyword work?
  • JavaScript - How to have collision with character…
  • For-each over an array in JavaScript
  • JavaScript hashmap equivalent
  • How to detect if multiple keys are pressed at once…
  • How to convert number to words in java
  • Property or method "key" is not defined on the…
  • Adding Input Value Into An Object (Vue.js)
  • Get the name of an object's type
  • Data binding between two Polymer elements using polymer 1.0
  • Issue: C compiler used for C++ in CMake | CMake + git
  • How to pass C# dictionary into typescript Map
  • Design DFA accepting binary strings divisible by a…
  • Palindromic numbers in Java
  • What does this symbol mean in JavaScript?
  • Object checking issue in JavaScript
  • Mismatch Detected for 'RuntimeLibrary'
  • How the int.TryParse actually works
  • Pass array and/or object data between Polymer elements
  • Javascript - removing undefined fields from an object
  • How do I correctly clone a JavaScript object?
  • How do I dynamically assign properties to an object…
  • What is the scope of variables in JavaScript?
  • a function to create and add to an object
  • Svelte with leaflet
  • Programmatically generate url from path and params
  • Logging best practices
  • error LNK2038: mismatch detected for '_MSC_VER':…
  • CSS animation as a Google Map Marker (Polymer 1.0)?
  • How to iterate through property names of Javascript object?
  • What's the best way to get the last element of an…
  • How do I merge two dictionaries in a single…
  • Filtering an object based on key, then constructing…
  • Test for existence of nested JavaScript object key
  • JavaScript: How to pass object by value?
  • How to create a function which converts an array…
  • Clearing a polyline on a Polymer Google Map
  • How does PHP 'foreach' actually work?
  • Math.min with map returns NaN on 0 in collection
  • Start redis-server with config file
  • Vue v-bind:class not working immediately when…
  • Map vs Object in JavaScript
  • Remove leading zeros from a number in Javascript
  • Cannot read property 'nodeName' of null at…
  • Why can I index by string to get a property value…
  • Program.Mattor(): not all code paths return a value.…
  • Database development mistakes made by application developers
  • How to convert XML to java.util.Map and vice versa
  • How to serialize SqlAlchemy result to JSON?
  • How to save reference to "this" in Vue component?
  • How do you clear the SQL Server transaction log?
  • Extract object keys by type of the property
  • Google Map API V3 not working inside polymer-element
  • How to parse an RSS feed using JavaScript?
  • Converting an Object to an array of KV values
  • How this line in code works ( reduce, groupbyid)
  • Am I paranoid? "Brutally" big Polymer website after…
  • Javascript counting number of objects in object
  • remove objects from array by object property
  • What are access specifiers? Should I inherit with…
  • Vue - component props not watching object changes properly
  • Good MapReduce examples
  • How to place object files in separate subdirectory
  • Deleting an element from an array in PHP
  • JQuery - Get select value
  • Is there a way to link with image in Vue?
  • Copying an array of objects into another array in javascript
  • Adding json response to a Map field Mongoose Node
  • Using a value convertor on an ES 6 Map in Aurelia
  • EL access a map value by Integer key
  • TypeScript: Hacking around unsoundness in a Map…
  • javascript functional programming External…
  • Why isn't my ternary operator working in Jsx?
  • Function runs into an error after successfully…
  • javascript removey key pairs from an array of objects
  • How to get main JQuery Mobile page to display after…
  • Pass id from component to service.ts Angular
  • Replace the UUIDs with Strings
  • Vue js dynamically added property not reactive
  • Ember.js how does reopenClass work?
  • How to prevent Google Polymer from changing event.target?
  • ReferenceError: obj is not defined using…
  • Add polyline to array of markers in Leaflet
  • Update nested object in array in ReactJs
  • How to Compile Object files into an Archive using Make
  • How to set zoom level in google map

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Converting from a string to boolean in Python?

Next Post:

If isset $_POST

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error