I have heard that JavaScript does not support associative arrays. Is it true
I mean:
assocArray = [
{a: ""},
{b: ""},
{c: ""}
];
Run Code Online (Sandbox Code Playgroud)
how to get the keys of above assocArray?
I tried using:
for (each in assocArray) {}
Run Code Online (Sandbox Code Playgroud)
it gives: 0, 1, 2
You can try with flatMap()
The
flatMap()method first maps each element using a mapping function, then flattens the result into a new array. It is identical to amap()followed by aflat()of depth 1, butflatMap()is often quite useful, as merging both into one method is slightly more efficient.
and Object.keys()
The
Object.keys()method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop.
var assocArray = [{a : "" },{b : ""},{c : ""}];
var keys = assocArray.flatMap(i => Object.keys(i));
console.log(keys);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |