[[+content_image]]
V
V
vladeol2018-05-22 13:08:10
JavaScript
vladeol, 2018-05-22 13:08:10

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

[[+comments_count]] answer(s)
A
Anton fon Faust, 2018-05-22
@bubandos

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

B
Bhudh, 2018-05-22
@Bhudh

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

PS Zhi-shi write through and!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question