0
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

What should "? => ?" be for the desired result?

Edited

My question is after reading the documentation Array.prototype.map() in MDN.

My question is (not how to solve this) but to learn about the callback function in .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.

6
  • 4
    quiz pro quo ...? is it homework? Commented Dec 7, 2017 at 15:25
  • 2
    How much research effort is expected of Stack Overflow users before posting a Question? - Also see How do I ask a good question - Question that simple post requirements and ask for solution show no effort but expect all the free effort from others. Commented Dec 7, 2017 at 15:28
  • no its not homework, I was trying to extract the values from a JSON and I boiled it down to this question Commented Dec 7, 2017 at 15:33
  • how would you so eloquently formulate the problem where i want the values of an object mapped with an array to a new array @fran? with a poem? Commented Dec 7, 2017 at 15:37
  • 2
    Instead of asking what to do / you should show some research, too. / Otherwise it needs some luck / to analyze where you are stuck. :D Commented Dec 7, 2017 at 15:45

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.