1

I have one problem. Anyone help me out. I am new to React so I am not able to debug it.

The problem is

When I print this.props.colors i.e.

console.log(this.props.colors)

I got below result.

{"colors": [["Red", true], ["Blue", false], ["Green", true], ["Black", true], ["White", false]]}

But I want it below form

0: (2) ["Red", true]
1: (2) ["Blue", false]
2: (2) ["Green", true]
3: (2) ["Black", true]
4: (2) ["White", false]

How can I get this? Anyone help me out.

Thank you.

4
  • 2
    Like this this.props.colors.colors? Commented May 12, 2020 at 5:44
  • yes, exactly!!! Commented May 12, 2020 at 5:47
  • I tried that too. But it prints undefined. I tried it as follows console.log(this.props.color.colors) prints {"colors": [["Red", true], ["Blue", false], ["Green", true], ["Black", true], [White", false]]} but console.log(this.props.color.colors ? this.props.color.colors.colors : '1111111' ) prints undefined Did I miss something? Commented May 12, 2020 at 10:32
  • @Rich try Object.assign(). Here is working example: codesandbox.io/s/hungry-cannon-793fm Commented May 12, 2020 at 10:49

2 Answers 2

3

If data - this.props.colors is :

{
  "colors": [
     ["Red", true], 
     ["Blue", false], 
     ["Green", true], 
     ["Black", true], 
     ["White", false]]
}

try this.props.colors.colors[0] or this.props.colors['colors'][0]

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

1 Comment

this.props.colors['colors'][0]
1

Also you can format colors into more comfortable array.

Use const colors = Object.values(this.props.colors) then it will return array like

[
 Array(5)
    0: ["Red", true]
    1: ["Blue", false]
    2: ["Green", true]
    3: ["Black", true]
    4: ["White", false]
]

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.