-1

I have a map structure where key is array and values is objects.

myMap = new Map();
myMap.set(["_1030002", "_1030003"], { name: "Yayay", description: "aaa", floor: -3});

How to get values of objects by one key?

For example how to get value of 'floor' by key "_1030003"?

My provider store.ts:

import { HttpClient } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http'
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { JsonStoresDataInterface } from './../../providers/stores/stores';

@Injectable()
export class StoresProvider {

    private JsonStoresData = new Map();
    defaultKey  = "_1030003";
    url_request = "path/to/json";


  constructor(public http: HttpClient) {
  }

  getStoresRemote(){
        this.http.get<JsonStoresDataInterface>(this.url_request).subscribe(data => {
            var json = data.data;
            for (var store of json.stores){
                this.JsonStoresData.set(store.polygon_id, {
                        name: store.name,
                        description: store.description,
                        floor: store.floor[0],
                        image: store.pic_info.src,
                        uid: store.uid
                        })
            }
        });

                console.log(this.JsonStoresData);//ok map
                let keys = Array.from(this.JsonStoresData.keys());
            let key = keys.find(key => key.includes(this.defaultKey));
            console.log(this.JsonStoresData.get(key).floor);
       //TypeError: Cannot read property 'floor' of undefined


}
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • `console.log(???)` The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Jan 09 '19 at 08:33
  • at least a bit of effort for pasting your data structure well as source code..... – quirimmo Jan 09 '19 at 08:36

1 Answers1

3

You could use keys() method. The keys() method returns a new Iterator object that contains the keys for each element in the Map object in insertion order.

Since an Iterator object is returned, you need to use Array.from method.

We need to use includes method in order to find the map's key which contains key _1030003 as in your question.

myMap = new Map();
myMap.set(["_1030002", "_1030003"], { name: "Yayay", description: "aaa", floor: -3});
let keys = Array.from(myMap.keys());
let key = keys.find(key => key.includes("_1030003"));
console.log(myMap.get(key).floor);

Another problem you have is that you're not correctly handling asynchronous request. Since get operation is executed asynchronous, the console.log(this.JsonStoresData); will be executed before the request call finished.

One solution could be using a callback function.

getRequest(callback){
    this.http.get<JsonStoresDataInterface>(this.url_request).subscribe(data => {
        var json = data.data;
        for (var store of json.stores){
            this.JsonStoresData.set(store.polygon_id, {
                    name: store.name,
                    description: store.description,
                    floor: store.floor[0],
                    image: store.pic_info.src,
                    uid: store.uid
                    })
        }
        callback(this.JsonStoresData)
    });
}
 getStoresRemote(){
      let def_key = this.defaultKey;
      getRequest(function(data){
          let keys = Array.from(data.keys());
          let key = keys.find(key => key.includes(def_key));
          console.log(data.get(key).floor);
      });
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128