Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Key mapping works in JavaScript maps

Below is a console.log of a result set from Neo4j. It returns an array of maps.

I'm able to results[0].get('RESULT'), which returns the object or map (I'm unsure which one it is) for the RESULT key.

How does this work? When I create a map, I have a key and a value but no way to have an array of keys that when I call .get, it returns the "mapped" object.

 [ Record {
     keys: [ 'RESULT', 'PROVINCE', 'CITY' ],
     length: 3,
     _fields: [ [Object], [Object], [Object] ],
     _fieldLookup: { RESULT: 0, PROVINCE: 1, CITY: 2 }
    },
   Record {
     keys: [ 'RESULT', 'PROVINCE', 'CITY' ],
     length: 3,
     _fields: [ [Object], [Object], [Object] ],
     _fieldLookup: { RESULT: 0, PROVINCE: 1, CITY: 2 }
    },
   Record {
     keys: [ 'RESULT', 'PROVINCE', 'CITY' ],
     length: 3,
     _fields: [ [Object], [Object], [Object] ],
     _fieldLookup: { RESULT: 0, PROVINCE: 1, CITY: 2 }
    }]

When I console.log a map it looks like this:

Map {
    'RESULT' => { name: 'Bob' },
    'PROVINCE' => { name: 'BC' },
    'CITY' => { name: 'Nanaimo' }
}
like image 586
Cazineer Avatar asked Jun 18 '26 16:06

Cazineer


2 Answers

I'm assuming you are using the official Neo4j Javascript Driver.

In this case the result set you are handling is an array of Record objects. When you do results[0].get('RESULT') you are calling the get function implemented in this class.

The docs about Record.get function says:

Get a value from this record, either by index or by field key.

like image 143
Bruno Peres Avatar answered Jun 21 '26 06:06

Bruno Peres


You can use .entries() to get property, value pairs of the Map as an Iterator and Array.from() to get the properties as an array

const map = new Map();
map.set("RESULT", {name:"Bob"});
map.set("PROVICE", {name:"BC"});
map.set("CITY", {name:"Nanaimo"});

let entries = Array.from(map.entries(), ([prop]) => prop);

console.log(entries);

let prop = "CITY";

console.log(
  map.get(entries[entries.indexOf(prop)])
); // `{"name": "Nanaimo"}`
like image 29
guest271314 Avatar answered Jun 21 '26 06:06

guest271314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!