当key来自另一个数组时,如何使用键/值对从多维数组中访问值?我知道这两种技巧都没有回报!感谢
var keySource = {doo: "loo"}
var target = [{foo: "a", doo: "b"}, {foo: "c", doo: "d"}]
var key = Object.keys(keySource );
/*-------this doesn't work---*/
target[0]['key']; // no result
target[0].key; // no result
/*-------this works fine---*/
something[0]['doo']; // b
something[0].doo; // b
答案 0 :(得分:2)
key
本身实际上是一个数组,因此您需要使用索引
var keySource = {
doo: "loo"
}
var target = [{
foo: "a",
doo: "b"
}, {
foo: "c",
doo: "d"
}]
var key = Object.keys(keySource);
console.log(target[0][key[0]]);