Searching for array value inside another array and returning

0

Look at the scenario:

I have the following array's

let arr = [23,0,0]

let coresLinhasPorPocsag = ["#4d79ff", "#ff6600", "#00cc88", "#b31aff"]

I need to get the color according to the position of the array arr

I can get it from the first color, but when it goes back on it it returns with undefined can anyone help me find the error?

Example:

arr[0] = 23

coresLinhasPorPocsag[0] = "#4d79ff"
  

Result = 23 in color "# 4d79ff"

 arr[1] = 0 

coresLinhasPorPocsag[1] = "#ff6600"
  

Result = 0 in color "# ff6600"

And so on.

Follow the code below:

 "data": "somatoriaEcmUltimas24h",
                    "render": function (data, type, row) {
                        let linha = ''
                        let coresLinhasPorPocsag = ["#4d79ff", "#ff6600", "#00cc88", "#b31aff"]
                    let arr = (data.split(','))
                    for (var i = 0; i < arr.length; i++) {
                          linha += "<div class='row' style='color:'" + coresLinhasPorPocsag[arr[i].indexOf(arr[i].length)] +  "'>" + arr[i] +  "</div>"
                    }
                return linha
            }
        },
    
asked by anonymous 01.10.2018 / 22:01

2 answers

1

If I understood what you wanted, would that be?

function coresLinhasPorPocsag() {
  let linha = ''
  const coresLinhasPorPocsag = ["#4d79ff", "#ff6600", "#00cc88", "#b31aff"]
  const arr = [23,0,0]
  for (var i = 0; i < arr.length; i++) {
    linha += '<div class="row" style="color: ${coresLinhasPorPocsag[i]}">${arr[i]}</div>'
  }
  return linha
}

window.document.body.innerHTML = coresLinhasPorPocsag()
    
02.10.2018 / 00:19
1

ES6 introduced the "for-in". It allows you to scroll through the indexes of an array.

function exemplo() {
  let linha = '';
  const coresLinhasPorPocsag = ["#4d79ff", "#ff6600", "#00cc88", "#b31aff"];
  const arr = [23,0,0];
  for(let index in arr) {
    linha += '<div class="row" style="color: ${coresLinhasPorPocsag[index]}">${arr[index]}</div>';
  }
  return linha;
}
    
02.10.2018 / 05:15