Sorry maybe very easy for someone, but how can I get the value from this array?
this.optionSelect = [{value: 'aa', name: 'ccc'}];
I tried :
console.log( this.optionSelect['value']);
also `
console.log( this.optionSelect[0]);`
Sorry maybe very easy for someone, but how can I get the value from this array?
this.optionSelect = [{value: 'aa', name: 'ccc'}];
I tried :
console.log( this.optionSelect['value']);
also `
console.log( this.optionSelect[0]);`
They are multiple level to need to interact with.
First level is the array. You can see it's an array because of the delimiting characters [ ] :
[{value: 'aa', name: 'ccc'}
];
You access the first element of the array using an index, like : this.optionSelect[0]
Then you have to deal with an object. You can see it's an object because of the delimiting characters { } :
{value: 'aa', name: 'ccc'
}
To access an object, you have to use the name of the key you want, like : obj.value
or obj['value']
.
Both notation works.
Now do both in the same line :
this.optionSelect[0].value
You can reach this value by the following. Arrays are Indexed, the 0 is which index in the array you are accessing so. JavaScript Arrays.
this.optionSelect[0] will be -> {value: 'aa', name: 'ccc'};
Then add .value
as the key you want to access of the object. Property access.
console.log(this.optionSelect[0].value);
Get the value
of the first item in the array:
const value = this.optionSelect[0].value;
Also try destructuring:
const [{ value }] = this.optionSelect;