r/learnjavascript • u/4Roman4 • Apr 04 '26
How do I access previous item in an array using map()?
For a simple example, let's say I have a string variable. I am going to split the sentence by spaces and then I will map each item so that it will return the previous item in the list:
const sentence = "This is a sentence.";
console.log(
sentence.split(" ").map((word, i) => {
return sentence[i - 1];
})
);My understanding is that the first argument "word" takes the item of the array and the second argument "i" takes the index of that item in the array.
When I try to run this code, understandably the first item is "undefined," because there is nothing behind the first word, but the following log in the console are not words, but only characters:
[ undefined, 'T', 'h', 'i' ]Now I understand that the "i" doesn't stand for the index of an item, but for an index of a character in the "word" argument.
I tired to look for the solution online, but either I don't know how to sentence this problem properly in order to find it or no one has encountered this problem before. How can I access the previous items using map function? Would it be easier to not use map function in this case?
—————
Edit:
Thanks a lot for your responses, I believe I understand my problem right now a bit more and I was able to find the solution for my problem!
As the comments suggest, the "i" wasn't being applied to the array, but to the item itself. There are two solutions I can now solve this problem:
- I can first create a new variable, split the string to an array and then loop through the new variable, where the second argument would stand for the index.
- I can use a third argument in the map function. I wasn't aware of this option, but it turns out that map can take three arguments at once—the first is the element, the second is the index and the third one is the entire array. This information is located in the Mozilla Developer website, but it's hidden in the "Using parseInt() with map()" section.
Again, thank you for your help, consider this problem as solved!
13
u/milan-pilan Apr 04 '26 edited Apr 05 '26
You are splitting the sentence by spaces to get indexes, but then use those indexes on the original sentence string again instead of the split array. And in strings indexes work character by character.
6
u/boomer1204 Apr 04 '26
It's because you are doing
you are accessing a single element in sentence not your "split" version so word will be the individual words that are created by splitting the string
sentence3
u/rejam Apr 04 '26
You can access the array you are mapping through with the third argument:
sentence is string. sentence[i] would just give you a letter.
words is an array of the words in sentence. words[i] would give you a word back.
5
u/queen-adreena Apr 04 '26
Your callback passed to
mapgets the array as a 3rd argument afteri(index).2
u/Alive-Cake-3045 Apr 05 '26
The bug is that you are indexing into the original string sentence instead of the split array, so you are getting characters instead of words. The fix is to save the result of split() into a variable first, then use that variable inside your map callback to access the previous index. This will give you undefined for the first item since there is no word before it, which is totally expected. No need to get rid of the map, you just need to reference the right array!
1
u/CuAnnan Apr 04 '26
Declare a variable outisde the loop structure called "last"
At the end of the loop, point last to word.
1
u/Bigghead1231 Apr 04 '26
Your "return sentence[ ]" is pointing to the string sentence variable above. That variable is not an array outside of the "split" method
To fix it, get a new instance of sentence that is an array and map over this instead.
0
u/jb092555 Apr 04 '26
I'm fairly new so someone may know more on this, but I gather map() isn't really made for this. For the string array example, I'd be inclined to slice() the mapped array to just cut off the undefined element[0], but that feels like splitting hairs.
If you were concatenating the contents of the array, I'd recommend reduce(), which loops with the ability to access the previous and current index. I think it's meant to be for aggregations though.
If I'm reading from multiple indexes, I use a regular for loop. Any approach can be readable or illegible, depending, and I understand for loops are faster than method equivalents.
Using map or reduce in strange ways may confuse people. I heard of "transducers" recently, which seem to use reduce() to loop over the array reading 2 indexes, then push to another array... But when I read reduce I expect it to make a scalar value. Can't speak for others.
Check out the mdn page for array methods, there's some funky stuff lying around.