0

I read a json file (basically flat table) and dynamically nest the data. I get nested data similar to:

[{key: "AAA", value: 100}, {key: "BBB", value: 200}, {key: "CCC", value: 150}]

Accessing key and/or value by index is no problem, e.g. dat[0].key, dat[1].value. But how do I directly access a value by not using (because not knowing) its index. I.e. dat["BBB"].value or dat["AAA"].value

What is the usefulness of key/value pairs, if I still have to access the data via indexes. In my case this would mean, to first somehow determine the index of a specific key and then reference by key.

I am quite sure, this is a very stupid question and I apologize for it, but after several days of searching the net, I still was not able to find an answer.

Thanks an awful lot for any help

PaLi
  • 73
  • 9
  • 2
    Using array find, `arr.find(x => x.key = 'AAA')` will return `{key: "AAA", value: 100}` (only the first instance) – evolutionxbox Oct 11 '17 at 09:06
  • That doesnt work... You used `=` not `===` so `arr.find(x => x.key = 'BBB')` will just return the first item – notrota Oct 11 '17 at 09:10
  • @gerardofutrado The dupe target only deals with objects, not an array of objects. – evolutionxbox Oct 11 '17 at 09:13
  • @evolutionxbox here is another one. The fact is that we don't need this answered by the zillionth time, again. – Gerardo Furtado Oct 11 '17 at 09:16
  • @GerardoFurtado fair enough. Thank you for updating the dupe target – evolutionxbox Oct 11 '17 at 09:17
  • 1
    @gerardo furtado: Well, I see the relation, but did not find that five year old answer earlier. If so, I might have found my own solution without posting a question. Please do not feel urged to answer a question. If this has been questioned a "zillionth" time, then maybe because it is neither intuitively to solve nor properly documented. I needed an answer - and I honestly thank all who invested their time to help me. – PaLi Oct 11 '17 at 13:31
  • @PaLi it's not your fault, you certainly can ask that question. My comment was addressed to the answerers that, instead of closing a question which is clearly a duplicate (by the way, this is very well documented), decided to answer it. – Gerardo Furtado Oct 11 '17 at 23:11

5 Answers5

1

A form of a map can be realized in JS via Objects.

Here's an example:

var data = {
  "key": "value"
}

console.log(data.key); // prints 'value'
console.log(data["key"]); // prints 'value'

You could parse the data into an object like:

function parseDataIntoObject(dataArray) {
  var data = {};
  dataArray.forEach(
    (object) => {
      data[object.key] = object.value;
    }
  );
  return data;
}

then you can fetch your data like data["AAA"].

I can provide more examples on the subject if needed.

anteAdamovic
  • 1,462
  • 12
  • 23
  • Thanks very much! Well that's actually what I wanted, and I assumed I have an array of objects. I see the proposed solutions, but it just frustrates me more - what is the added value of key / value pairs if I cannot access a value by its associated key. – PaLi Oct 11 '17 at 13:25
  • Because a Map is not supported in JavaScript by default, you simply had an array of objects which doesn't qualify as a Map. If you find my answer helpful please mark it as correct. – anteAdamovic Oct 11 '17 at 13:27
1

Using the Array find method:

let data = [{key: "AAA", value: 100}, {key: "BBB", value: 200}, {key: "CCC", value: 150}]

let item1 = data.find(x => x.key === 'AAA')
let item2 = data.find(x => x.key === 'BBB')
let item3 = data.find(x => x.key === 'CCC')

console.log(item1, item2, item3)

Please note: This will only return the first instance of an object with the matching key. For multiple objects, use one of the other solutions posted here.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
0

Try something like

function getByKey( arr, key )
{
   var results = arr.filter( function( item ){ item.key == key });
   var value = results ? results[0].value : "" 
   return value; 
}

getByKey( data, "AAA" ); //this will return the value for AAA
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Well, quite an effort for a functionality that I intuitively had expected to be available. Obviously not, but I guess I will do exactly what you propose. "Outsourcing" the issue and use my own function. - Thanks! – PaLi Oct 11 '17 at 15:44
0

This returns the first object matching with the key. You can change this according to your requirement if you want an array of multiple matching objects

 var data = [{key: "AAA", value: 100}, {key: "BBB", value: 200}, {key: "CCC", value: 150}];
 var keyToFilter = "AAA";//could be anything

 var resultObj = data.filter(function(obj){return obj.key === keyToFilter })[0];
Rohit Agrawal
  • 1,496
  • 9
  • 20
0

do

const arr = [{key: "AAA", value: 100}, {key: "BBB", value: 200}, {key: "CCC", value: 150}]

const item = arr.filter(obj => obj.key === "BBB")[0];

console.log(item.value);

Basically, this filters the array for the same key, then get the value by the first item

if there is no item, test for the following

if (item.length > 0) // ...
notrota
  • 1,048
  • 10
  • 21