JavaScript How to transform JSON keys into value transform json key value,transform json key

I want to transform following format JSON:

{
    id1: value1,
    id2: value2
    id3: value3,
    ...
} 

into

[    
    { id: 'id1', value: 'value1' },
    { id: 'id2', value: 'value2' },
    { id: 'id3', value: 'value3' },
    ...
]

What would be fast and convenient? Is there any way we can use underscore.js for this. Thank you in advance.

Answer:1

You can use the map method provided by underscore.

  var input = {
    "id1": "value1",
    "id2": "value2"
  };

  var output = _.map(input, function (value, key) {
    return {id: key, value: value};
  });
Answer:2

Something like:

var arr = Object.keys(obj).map(k => ({id:k, value:obj[k]}));

no underscore required ;-) If you're not using babel or something similar you might need to write:

var arr = Object.keys(obj).map(function (k) { return {id:k, value:obj[k]};});
Answer:3

User the following:

var arr = [];

var obj = {
    id1: "42",
    id2: "hello",
    id3: 6,
}

for (var i in obj) {
    arr.push({id: i, value: obj[i]});
}

Alternatively:

Object.keys(obj).map(function(value, index) {
   return {id: value, value: obj[value]}
})
Answer:4

Couldn't you just parse it using:

var object = JSON.parse(jsonString);

If that doesn't produce the desired result then just iterate through your string and manually add in the keys.

Answer:5

You should use Object.prototype.hasOwnProperty to ensure that you are only adding the keys that are directly set on obj:

var arr = [];
for (var key in object) {
    if (object.hasOwnProperty(key)) {
        arr.push({ id: key, value: object[value] });
    });
}
Answer:6

Using underscore, you could do this with the map method:

var obj = {
    id1: 'value1',
    id2: 'value2',
    id3: 'value3',
}

//with underscore, transforming the array
_.map(obj, function(value,key){
    return({
    id: key.toString(),
    value: value
  })
})
//obj = Object {id1: "value1", id2: "value2", id3: "value3"}

Here's a jsfiddle to test

Underscore map

Answer:7

I have added few elements with "visibility:hidden; position:fixed;" to the top of the page and for some reason, when I launch the script in Chrome, it makes "#pageOverlay" visible, but fails to unhide ...

I have added few elements with "visibility:hidden; position:fixed;" to the top of the page and for some reason, when I launch the script in Chrome, it makes "#pageOverlay" visible, but fails to unhide ...

I'm trying to figure out if this is a possibility. Concept is that there is a need for FAB actions to stay open after clicked. They need is that i want to have a classic FAB angular material FAB ...

I'm trying to figure out if this is a possibility. Concept is that there is a need for FAB actions to stay open after clicked. They need is that i want to have a classic FAB angular material FAB ...

I want create live audio streaming by websockets. This what works for me is listening microphone, create PCM, load PCM to BufferSource and playback. This what doesn't work is send PCM by websockets ...

I want create live audio streaming by websockets. This what works for me is listening microphone, create PCM, load PCM to BufferSource and playback. This what doesn't work is send PCM by websockets ...

  1. streaming audio audio recorder
  2. audio streaming radio hindvani
  3. audio streaming radio

I am using angular2 http.post in the following service: import {Injectable, Inject} from 'angular2/core' import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http'; ...

I am using angular2 http.post in the following service: import {Injectable, Inject} from 'angular2/core' import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http'; ...