Attempt to manipulate values of a vector generated by a JSON

0

I'm trying to "change" values of a received vector from the consumption of a JSON. My logic was to create a for to go through the vector, parse the item on that partition and rewrite it in a copy vector, thus having a vector with the values I want.

    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 every time. What do you think?

    
asked by anonymous 08.03.2017 / 00:51

3 answers

0

08.03.2017 / 01:50
0

In my view, the problem is with your Json. The data.day.lenght is being zeroed because, if it happened once there, its vector copia would have given.

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

    
08.03.2017 / 02:53
0

If copia is an array, you should expose it in JSX to concatenate it as a string, a good way to do this is with spread operator :

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

Another approach is to iterate through copia , transformed it into 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 copying is being populated.

Good luck!

    
08.03.2017 / 20:50