JavaScript JavaScript: Convert array of objects into hashmap javascript convert array to string,javascript convert array to object,

I have a set of values in an array where each value has an ID and LABEL.

Once I have the value array and type console value[0] and value[1], the output is:

value[0] 
Object {ID: 0, LABEL: turbo}

value[1] 
Object {ID: 1, LABEL: classic}

How can I store these values in a hash map like a key-value (ID-LABEL) pair, and store them in a json?

Answer:1

This could be achieved by calling reduce on your array of values (ie data), to obtain the required hash map (where ID is the key and value is the corresponding LABEL):

const data = [
{ID: 0, LABEL: 'turbo'},
{ID: 1, LABEL: 'classic'},
{ID: 7, LABEL: 'unknown'}
];

const hashMap = data.reduce((result, item) => {
  return { ...result, [ item.ID ] : item.LABEL };
}, {});

const hashMapJson = JSON.stringify(hashMap);

console.log('hashMap', hashMap);
console.log('hashMapJson', hashMapJson);
 
/*
More concise syntax:
console.log(data.reduce((result, { ID, LABEL }) => ({ ...result, [ ID ] : LABEL }), {}))
*/
Answer:2

You can iterator over each item in the array, and use the ID proeprty as a javascript objects key and the LABEL as the value.

var value = [{ID: 0, LABEL: "turbo"}, {ID: 1, LABEL: "classic"}];

let theNewMap = {};
for(var i = 0; i < value.length; i++) {
  theNewMap[value[i].ID] = value[i].LABEL;
}

// theNewMap Should now be a map with 'id' as key, and 'label' as value
console.log(JSON.stringify(theNewMap ))
Answer:3

You can use forEach method.

> var hmap = {};
undefined
> var value = [{ID: 0, LABEL: "turbo"}, {ID: 1, LABEL: "classic"}]
undefined
> value.forEach(function(element){
... hmap[element.ID] = element.LABEL;
... });
> hmap
{ '0': 'turbo', '1': 'classic' }

or

var value = [{ID: 0, LABEL: "turbo"}, {ID: 1, LABEL: "classic"}]
var hmap = {};
value.forEach(function(element){
    hmap[element.ID] = element.LABEL;
});
console.log(hmap);
Answer:4

I'm am very new to programming and I am trying to solve a problem where I have to convert an array of Fahrenheit values, to Celsius. Though I am not too sure where to start with .map and I would ...

I'm am very new to programming and I am trying to solve a problem where I have to convert an array of Fahrenheit values, to Celsius. Though I am not too sure where to start with .map and I would ...

  1. using mind map to write a book

I am trying to set up the following architecture: a core React application that gets built with some basic functionality, and the ability to load additional React components at runtime. These ...

I am trying to set up the following architecture: a core React application that gets built with some basic functionality, and the ability to load additional React components at runtime. These ...

I have a chart that shows the time in and time out for the employees on a daily basis. I've fetched the data from the database and all was working well. But now the columns show the data incorrectly ...

I have a chart that shows the time in and time out for the employees on a daily basis. I've fetched the data from the database and all was working well. But now the columns show the data incorrectly ...

I've got a tree like: { "nodes": [ { "id":1, "children":[ { "id":3, "children":[ {"id":4, "children":[]}, {"id":5, "children":[{"...

I've got a tree like: { "nodes": [ { "id":1, "children":[ { "id":3, "children":[ {"id":4, "children":[]}, {"id":5, "children":[{"...