我想获取数组中某个键的值。
arr= [
{key: "a", label: "Adam", value: "121"},
{key: "e", label: "TT", value: "44"},
{key: "ad", label: "RRR", value: "555"}
]
arr2 = ["a","e","ad"];
arr2.map((item) => {
//here value of item could be oneof the values in arr2
//say its "a"
//how do I get the value of key-"label" corresponding to the value in item???
// in this case the value of label returned should be "Adam"
result = get(arr, [item, "label"]); // is this possible??
});
对此有什么想法吗? 谢谢!
最佳答案
您可以在 arr
中找到
该条目,但请注意,这效率很低:
arr2.map(item => arr.find(e => e.key === item).label)
更好的解决方案是将 arr
转换为映射,其中键取自每个对象的 key
属性。
关于javascript - 如何获取数组中 "key"的值-Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48718457/