Searching array value within another array and return

Asked

Viewed 231 times

0

Take a look at the scenery:

I have the following array’s

let arr = [23,0,0]

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

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

I can pick up the first color, but when it passes again on it returns with undefined someone can help me find the mistake ?

Example:

arr[0] = 23

coresLinhasPorPocsag[0] = "#4d79ff"

Upshot = 23 in color "#4d79ff"

 arr[1] = 0 

coresLinhasPorPocsag[1] = "#ff6600"

Upshot = 0 in color "#ff6600"

And so on and so forth.

Follows code:

 "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
            }
        },
  • I can’t understand your doubt, if I explain it better I’ll find a way to help you.

  • @Marconi I need to associate the color array index with the list array index. I edited the question with example

  • ...+ coresLinhasPorPocsag[i] ?

  • @bfavaretto already tried, it didn’t work... because on arra[i] will be the number of the list and not its vector ;s

  • 1

    I edited it, wouldn’t it + coresLinhasPorPocsag[i]?

  • @bfavaretto truth, I wanting to do some very 'beautiful' I ended up running away from the simple, just pass the contactor... It worked.

Show 1 more comment

2 answers

1


If I understood what you want, it would be this?

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

1

ES6 introduces "for-in". It allows going through the index’s 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.