Answer the question
In order to leave comments, you need to log in
[[+content_image]]
How to add text to every 1 element in an array string?
Hello
I take an array of 3 lines, 4 elements: prntscr.com/jl1tpj
Log: [[1.0, apples, site.com/yabloki, 1$], [2.0, pears, site.com/grusha, 2$], [3.0, vologda, site.com/vologda, immoral]]
I send this array to email (this is all within the framework of google scripts, there is an opportunity there), I get: prntscr.com/jl25kt
I would like to receive: prntscr.com/jl267j
i.e. remove commas, add spaces, periods, etc. (the transfer has already been suggested how to do it), but I don’t know if there is even a possibility of such work with arrays in JS
I would be glad to help
Thank you!
Answer the question
In order to leave comments, you need to log in
I don't see any difficulty at all.
var result = "";
arr.split(',').forEach(function(entry, index) {
switch((index + 1) % 4) {
case 0:
result += entry + "<br>";
break;
case 1:
result += parseInt(entry, 10) + ".";
break;
case 2:
result += entry + "-";
break;
case 3:
result += "<a href=\""+entry+"\">"+entry+"</a">;
break;
}
});
console.log(result);
const LOG = [[1.0, "яблоки", "site.com/yabloki", "1$"], [2.0, "грушы", "site.com/grusha", "2$"], [3.0, "вологда", "site.com/vologda", "аморально"]];
/* Вариант 1 */
const StringifiedLOG = LOG.map( el => `${el[0]}. ${el[1]} - ${el[2]} - ${el[3]}` ).join( '<br>' );
/* Вариант 2 */
const StringifiedLOG = LOG.map( el => `${el[0]}. ${el.slice(1).join( ' - ' )}` ).join( '<br>' );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question