0

I am new in Angular 2. I have an object attributes which it is an instance of Map

I want to access to its value.

In the console, it shows: attributes map instance

InputFirstComponent

export class InputFirstComponent implements OnInit {

@Input() public inputFirstToDisplay: CustomInput;
constructor() { }
 ngOnInit() {
   console.log('InputFirstComponent: onInit');
   console.log(this.inputFirstToDisplay);
   console.log('attribute Object which i want to acces to its attribute')
   console.log( this.inputFirstToDisplay.attributes);
 }
}

I tried

console.log( this.inputFirstToDisplay.attributes.get('minOccurs'));
 console.log( this.inputFirstToDisplay.attributes['minOccurs'].value); // 
  print undefined

But the object is founded: Attribute object has minOccurs and  maxOccurs

the object inputFirstToDisplay of CustomInput class:

export class CustomInput {

  constructor(public name: string, public text: string, public defaultText: 
    string,
          public complexType: boolean, public type: string, public children: 
         CustomInput[] = [],
          public isMultiValued: boolean,
          public values: string[] = [], public indicator: string, public 
          required: boolean,
          public isSelected: boolean, public 
          simpleTypeVarietyOrComplexTypeContent: number,
          public choiceContent: boolean, public inputQname: string,
          public attributes: Map<string, string> = new Map<string, string>() 
      ) {}
      }

attributes: Map(String, string) is an attribute of the custom input class and it which I want to access to its value.

Thanks

Abenamor
  • 278
  • 1
  • 4
  • 15

1 Answers1

1

If you are gonna use String as keys there is no reason to use a hashmap, every object in javascript, as well as typescript, is a simple map. The only reason you would need to use the Map class is if you need something other than a String as a key.

Please take a look at this question -> How is a JavaScript hash map implemented?

In your case try console.log( this.inputFirstToDisplay.attributes['minOccurs']); // without the .value

Eduardo Vargas
  • 8,752
  • 2
  • 20
  • 27