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?

4
  • you want to check the value is array of not correct? Commented Aug 28, 2017 at 11:22
  • 1
    you have methods like isArray() developer.mozilla.org/en/docs/Web/JavaScript/Reference/… - you can use simple check like result['value'].isArray() ? result['value'][0] : result['value'] Commented Aug 28, 2017 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. Commented Aug 28, 2017 at 11:25
  • Possible duplicate of Check if object is array? Commented Aug 28, 2017 at 11:25

1 Answer 1

2

you can use simple check like

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

edited the way of use this function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.