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?
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?
Share Improve this question edited Jun 25, 2019 at 8:39 Kamil Kiełczewski 92.9k34 gold badges395 silver badges370 bronze badges asked Jun 25, 2019 at 4:05 casillascasillas 16.8k21 gold badges123 silver badges227 bronze badges 2- Learn about what JSON is, understand that all JavaScript objects, including arrays and functions, are morally hash tables, and your question will disappear because it won't even make sense to you. – Aluan Haddad Commented Jun 25, 2019 at 4:14
- 1 Does this answer your question? Convert object array to hash map, indexed by an attribute value of the Object – superjos Commented Feb 1, 2022 at 10:03
4 Answers
Reset to default 3This 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 }), {}))
*/
Try (where h={})
data.map(x=> h[x.ID]=x.LABEL );
const data = [
{ID: 0, LABEL: 'turbo'},
{ID: 1, LABEL: 'classic'},
{ID: 3, LABEL: 'abc'}
];
let h={}
data.map(x=> h[x.ID]=x.LABEL );
console.log(h);
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 ))
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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744125074a4559577.html
评论列表(0条)