How do I store all the values traveled by for in a variable?

Asked

Viewed 60 times

-3

I have this code:

for(i=0; i<results.rows.length; i++){
    $('.chips').material_chip({
        data: [
            {tag: results.rows.item(i).descricao},
        ]
    }); 
}

results.rows.item(i).descricao gives me some values..

how do I save all these values in for in a single variable?

EX: var exemplo = valor1, valor2... Anyway, all the values he found in for. It is possible?

  • 1

    Please do not duplicate your questions. If no one has answered previous, focus your efforts on it to make it clearer. For example, there left a comment with a suggestion, you even tried?

  • Yeah, it didn’t work

  • 1

    Then the ideal would be edit the question explaining that you tried (putting the code) and what happened (if there was an error, what the message, etc., that is, being specific, because "it didn’t work" is a vague description). You may even get answers here, but posting the question again isn’t ideal (because it might be closed because it is duplicated). I suggest you do the [tour] and read the page [Ask] and the help center

  • 1

    Putting examples also helps: some values of what you have in this array, what should be the result and what happened when you ran your code. In other words, a [mcve] <-- read this link too, it will help you better understand how to improve your questions and increase the chances of an accurate answer.

  • 2

    You need to study on arrays, the colleague gave you a useful answer and you are discarding it due to ignorance of the operation of arrays and the method push. Look for some material (example) and study a little more.

1 answer

2

You can store in an array

let descricoes = []

for(i = 0; i < results.rows.length; i++){
  $('.chips').material_chip({
    data: [
      {
        tag: results.rows.item(i).descricao
      },
    ]
  });

  // Aqui eu preencho o array
  descricoes.push(results.rows.item(i).descricao)
}
  • This returns me the amount of elements, not the values

  • Just go through the array you will get the values, example, descriptions[0], you get the first value, descriptions[1] the second and so on.

Browser other questions tagged

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