0

Console output:

{
  295798: {
    box_type: "QB",
    color: "Assorted",
    floor_allowed: 2100,
    grade: false,
    head_size: null,
    is_special: "0",
    length: false,
    live_inventry: "1",
  }
}

I have an array that is shown in the screenshot above in chrome console. when I print out the array it comes up.

console.log(props.thisData);
console.log(props.thisData[0].color);

props.thisData shows the whole array. However when I try to access the color I get the following error:

Product.jsx:56 Uncaught TypeError: Cannot read property 'color' of undefined

1 Answer 1

1

The data structure in your console output is not an Array. Its an Object with numbers as keys. It seems that there is no element with key 0 (no property 0 in the object). Hence, you get undefined.

To access your color property you would have to access it through the key on your object:

props.thisData['295798'].color

UPDATE

I suggest you to convert your object into an array and add the key as an id to each element if you want to work with the Array.

const dataArray = Object
  .keys(props.thisData)
  .map(key => ({ id: key, ...props.thisData[key] }))
Sign up to request clarification or add additional context in comments.

3 Comments

any idea on how I can get the info then?
that number is different for each product, so how can I a variable instead of the number itself? props.thisData[ID_HERE].color /
@mcjanty32 just convert it to an array as in the update

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.