map() output/return used as next input (class method)

I hope the answer to my question is not recursion. I need to use the result of the 1st iteration of a map method as the input for the next iteration, up to the last item in the array. I'm using map() inside a method that is part of my class. I have to create a new instance of the class that uses 1st an initial array of strings, and then again for the output/return value of that first instance. That output is the input for the 2nd instance of my class and its output is used in the 3rd, and so on. But I know that is not a good approach even though it works. So how can I get the return value for the first item in a map method to be the input for the next item?
12 Replies
13eck
13eck•2y ago
Can you be a bit more specific? If each input is expecting an array you can chain map()s to the initial array until you get the final array. Or, if you want to do only one loop you could use .reduce() and have all the different mutations take place in one iteration. But without a more specific explanation of what you're looking for (along with at least a bit of code) there's not much help to be had.
Kernix
Kernix•2y ago
Okay, I can try that. I was hoping to use the class for all scenarios but the number of times I need to run the class method will be different.
Kernix
Kernix•2y ago
Here is a my codepen for what I am doing: https://codepen.io/jim-kernicky/pen/KKQebjW
Kernix
Kernix•2y ago
I already had some people laugh at my code, so there's that. Specifically, I need to run a number of regular expressions again an array of lines of code, and use replace() to add a span tag around the match. My HTML regex runs works (with 1 issue) but it's 5 regular expressions. There is 1 CSS RegEx that is proving very difficult, and without it I have 9 regular expressions to run for CSS lines. I'm really getting messed up on how do the output as input thing, so I'm reading up on how to grab the lines of code from a file, rather than my present scenario of adding the lines to an array.
13eck
13eck•2y ago
If you have an array of regexs and an array of code lines you could do a nested loop. Something like this:
// this array holds the patterns and replacement strings
const regexps = [[pattern, replacer], [pattern, replacer], [pattern, replacer]];
// this array holds the lines you want to compare against
const codeLines = []

// use array destructuring to get named values
regexps.forEach( ([ptn, rpc]) => {
return codeLines.map( (line) => {
line.replaceAll(ptn, rpc);
});
});
// this array holds the patterns and replacement strings
const regexps = [[pattern, replacer], [pattern, replacer], [pattern, replacer]];
// this array holds the lines you want to compare against
const codeLines = []

// use array destructuring to get named values
regexps.forEach( ([ptn, rpc]) => {
return codeLines.map( (line) => {
line.replaceAll(ptn, rpc);
});
});
Does off the top of my head so it might need some tweaking. Should be a good starting point, though
Kernix
Kernix•2y ago
Okay, that's something to work with. 1) the nested array is something I was thinking of but not for the way you have it. 2) Returning a map() inside a forEach() and destructuring the nested array is nice.
13eck
13eck•2y ago
đź‘Ť
Kernix
Kernix•2y ago
I tried nesting a map inside of a map but I was getting errors or no data. Let me give that a shot
13eck
13eck•2y ago
Oh, shoot…it might need to be a nested .map() inside a .map()….forEach() won't return anything…hmm Like I said, it's a starting point >_<
Kernix
Kernix•2y ago
That's why I was pushing to an empty array. I originally was using forEach. So now you think it needs to be a nested map or a chained map?
13eck
13eck•2y ago
If you're working with two arrays it needs to be nested, not chained
Kernix
Kernix•2y ago
I tried a nested map and also a for loop inside a map, but the output was wrong, which I guess means I did it wrong. But if you think that is my best chance, I'll try it. I also searched for using a recursive map but I did not find anything. I also tried a forEach where I tried to do a new Class but I was still left with the need for an output as the next input.
Want results from more Discord servers?
Add your server