我有一个 JS map
对象(这里是 JS 的新对象),它是一个数组数组,如下所示:
scores=[["key_1",true],["key_2",false],["key_3",false]....["key_n",true]]
我可以像这样访问控制台上的值:
scores[0] //outputs ["key_1",true]
scores[0][0] //outputs "key_"
//..and so on.
但是,我如何(快速)查找对象数组中的特定键,例如:
scores[["key_3"]] //obviously wont work
//expected output: false
最佳答案
使用Object.fromEntries()
将数组转换为对象。
使用Object.entries()
变回数组。
var scores = [
["key_1", true],
["key_2", false],
["key_3", false],
["key_4", true]
];
scores = Object.fromEntries(scores);
console.log(scores.key_3);
关于 map 对象的javascript查找值是数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59506530/