[英]How do I .map() a new array of values from an array and an object?
let a1 = [1, 3, 5];
let a2 = {1: "a", 2: "b", 3: "c", 4: "d", 5: 2,}
// pass a function to map
const map1 = a1.map(? => ?);
console.log(map1);
// expected output: Array ["a", "c", 2]
I want the number in the array 1, 3, 5 to map to the values on the keys of a2 我希望数组1、3、5中的数字映射到a2的键上的值
What should "? => ?" “?=>”应该是什么? be for the desired result? 为预期的结果?
My question is after reading the documentation Array.prototype.map()
in MDN. 我的问题是在阅读MDN中的文档Array.prototype.map()
之后。
My question is (not how to solve this) but to learn about the callback function in .map()
: 我的问题是(不是解决方案),而是要了解.map()
的回调函数:
let a1 = [1, 3, 5];
let a2 = {1: "a", 2: "b", 3: "c", 4: "d", 5: 2,}
// pass a function to map
const map1 = a1.map(a2, e => a2.value);
console.log(map1);
// expected output: Array ["a", "c", 2]
Here I wanted e in a1 to match a2 and return a2 in the map function, but it's clearly wrong but after Googling for examples I guess I just had to make a question about it and try not cause too much confusion. 在这里,我希望a1中的e匹配a2并在map函数中返回a2,但这显然是错误的,但是在使用谷歌搜索示例之后,我想我只需要提出一个问题,就不要造成太多的困惑。
Simply get the property from the object. 只需从对象获取属性。
let a1 = [1, 3, 5]; let a2 = {1: "a", 2: "b", 3: "c", 4: "d", 5: 2,} // pass a function to map const map1 = a1.map(k => a2[k]); // -----------------^^^^^^^^^^---- console.log(map1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.