Attempt to manipulate values of a vector generated by a JSON

Asked

Viewed 361 times

1

I am trying to "change" values of a vector received from the consumption of a JSON. My logic was to create a for to traverse the vector, parse the item on that partition and rewrite into a copy vector, thus having an array with the values I wish to.

    let copia = [];

for(i = 0; i< data.day.lenght; i++){     
  switch(data.day[i]){
    case 'Mon':
      copia[i] = 'Segunda';
    default:
      copia[i] = 'Não'
  }
}

return(   
   <Text style = {styles.welcome}>Day: {copia}</Text>)

The problem is that my vector copy is coming out blank always. What do you think?

  • You can post the JSON output for structure analysis?

3 answers

0

If the copia is an array, you should expose in JSX to be concatenated as string, a good way to do this is with the Operator spread:

<Text style = {styles.welcome}>Day: {...copia}</Text>)

Another approach is to iterate in copia, transfornado-it in JSX nodes:

let copia = [];

for(i = 0; i< data.day.lenght; i++){     
  switch(data.day[i]){
    case 'Mon':
      copia[i] = 'Segunda';
    default:
      copia[i] = 'Não'
  }
}
let copiaNodes=copia.map((copiaItem) => {
   return <Text style = {styles.welcome}>Day: {copiaItem}</Text>
});

return (<View>{copiaNodes}</View>);

If symptoms persist, you can debug and see if copy is being populated.

Good luck!

0

Do so:

var vetor = [];
var obj = JSON.parse(objetoJSON);

for (i in obj){
// condição if
vetor[i] = obj[i].nomeDaPropriedadeNoObjetoJson (exemplo: obj[i].nome)
// elseif
vetor[i] = obj[i].nomeDaPropriedadeNoObjetoJson (exemplo: obj[i].email)
}

I’m not sure that’s it. Clarify your question further. Att.

0

The problem is in your Json. The data.day.lenght is coming zeroed because, if you passed once there, your vector copia would have given.

Try to use console.log(data.day); and console.log(data.day.length); before the for to see the consistency of the data.

Browser other questions tagged

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