1

Is there a way to get the array being generated by the map function, inside the map function? For e.g.

let arr = [1, 2, 3, 4];
arr.map(item => {
  console.log(currentArray);
  return item * item;
}

So, in the above example, if at line 3 (currentArray), I want below output, how can I get that.

[1]
[1, 4]
[1, 4, 9]

The "array" parameter in map callback, returns the original array on which map function is called. Or do I need to go conventional for loop route to achieve this?

2
  • Why not array#reduce? Commented Aug 7, 2020 at 16:01
  • Instead of using .map(), you could use forEach method. Declare an empty array which will hold the sub arrays and using forEach, iterate over the array, adding new sub array in each iteration. arr.forEach((num, i) => res[i] = i > 0 ? [...res[i-1], num * num] : [num*num]); P.S: res is initially an empty array that will contain the sub-arrays after forEach ends. Commented Aug 7, 2020 at 16:12

3 Answers 3

2

You could map the squares by using a closure over ta temporary result set.

let array = [1, 2, 3, 4],
    result = array.map((t => v => t = [...t, v * v])([]));

console.log(result);

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

4 Comments

That is defining reduce in terms of map. Excellent! I have learnt something. Thanks! You have been followed!
Ok, this is pretty interesting. I'll check this.
But if the asker wants really what s/he writes, it is just access to the t in every step, which your answer still satisfies. S/he was not specific about the return anyways.
I was wrong; map can be written in terms of reduce but not vice versa.
1

You can use reduce function as follow,

let arr = [1, 2, 3, 4];


arr.reduce((acc, item)=>{
   acc.push(item*item);
   console.log(acc);
   return acc;
}, []);



// SINCE YOU ARE ASKING HOW TO DO IT USING MAP
console.log('*******************************')

let arr1 = [1, 2, 3, 4];
const currentArray = [];
arr1.map(item => {
  currentArray.push(item * item);
  console.log(currentArray);
  return  item* item;
})

1 Comment

You can use the short version of map & reduce as show by other users. So I don't want to edit my answer.
0

You can use the reduce instead of the map, but you do not have to by the way. Anyways, then you will have your accumulator as the first argument to the reducer callback. The variable accumulator will hold the intermediate states of the value to be returned.

let arr = [1, 2, 3, 4];
let result = arr.reduce((accumulator, item) => {
  console.log("currentArray accumulator: ", accumulator);
  return [...accumulator, item * item];
}, []);

There is a neat another answer around which uses map and do what you want. So, yes it is possible to achieve same with map and reduce is a convenience function that actually is a special case map function itself.

4 Comments

reducer, change to reduce
It does not, well at the end. I return a new array at every step of it. I could very well use push to not return a new array at every step. But at the end there is no three arrays
I tested it and it does not produce the desired output. I did not downvote you btw
I have tested as well. You must be wrong it does produce the desired output. Desired out put is as in the question to have intermediate arrays in the callback function and the accumulator is it.

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.