0

I have a problem with representing of my values in my html table. I have array of values, most of them are strings, but while program works some of these become arrays. Something like I have here:

if(res.data.results[elem]['value'].toLowerCase() === nextProps.value){
    res.data.results[elem]['value'] = [res.data.results[elem]['value'], 'black'];
    res.data.results[elem]['another_value'] = [res.data.results[elem]['another_value'], 'black'];
}

I try to flag it with colors as you checked already. And then I print them in table, as I managed already, in this way:

<td style={{'color': result['value'][1]}}>{result.value[0]}</td>
<td style={{'color': result['another_value'][1]}}>{result.another_value[0]}</td>

When I have only string in my value I get first letter of my word/sentence/whatever. Is there another way to print whole string when value is not an array instead of making arrays from all values at the start?

Alex
  • 1,221
  • 2
  • 26
  • 42
  • you want to check the value is array of not correct? – Mayank Shukla Aug 28 '17 at 11:22
  • 1
    you have methods like `isArray()` https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray - you can use simple check like `result['value'].isArray() ? result['value'][0] : result['value']` – Kasia Aug 28 '17 at 11:23
  • But I wanna to do it in my style if it's possible and then take first element if true and whole string if false. – Alex Aug 28 '17 at 11:25
  • Possible duplicate of [Check if object is array?](https://stackoverflow.com/questions/4775722/check-if-object-is-array) – Mayank Shukla Aug 28 '17 at 11:25

1 Answers1

2

you can use simple check like

Array.isArray(result['value']) ? result['value'][0] : result['value'] 

edited the way of use this function

Kasia
  • 1,665
  • 1
  • 10
  • 16