I want to convert mine Objectto an array, here is my object.
Object
{5.0: 10, 28.0: 14, 3.0: 6}
I want an array as below
[{"type": 5.0,"value":10},{"type": 28.0,"value":14}, {"type": 3.0,"value":6}]
or
[{"5.0": 10},{"28.0": 14}, {"3.0": 6}]
Get the keys through Object.keys, and then use the function mapto get the desired result.
Object.keys
map
const obj = {5.0: 10, 28.0: 14, 3.0: 6}; const mapped = Object.keys(obj).map(key => ({type: key, value: obj[key]})); console.log(mapped);
Another solution can be provided through Object.entriesand restructuring the array.
Object.entries
const obj = {5.0: 10, 28.0: 14, 3.0: 6}; const mapped = Object.entries(obj).map(([type, value]) => ({type, value})); console.log(mapped);
Use Object.keys and array.map:
var obj = {5.0: 10, 28.0: 14, 3.0: 6} var arr = Object.keys(obj).map(key => ({type: key, value: obj[key]})); console.log(arr);
And if your browser supports Object.entries, you can use it:
var obj = {5.0: 10, 28.0: 14, 3.0: 6} var arr = Object.entries(obj).map(([type, value]) => ({type, value})); console.log(arr);
Source: https://habr.com/ru/post/1694847/More articles:Excel converts URLs to images (1004) - vbaA simplified / faster / more elegant way to split a string at user positions - pythonHow to check the fast version for the playground? - iosСоздайте DataFrame комбинаций для каждой группы с pandas - pythonHow to set image size on excel webbrowser - htmlpromise and promise refactor.all with asynchronous wait es2017 - javascriptHow to insert an image from the Internet into a VBA Excel Userform - internet-explorerThe difference between values and reference arguments in Rcpp - c ++How to convert a method of passed arguments to a single object in Java - javaXcode 9.2 does not show Swift 4.1 - xcodeAll Articles