0

Say I want to console.log the name value in the following state in React:

state = {
   editingToggle: false,
   list : [
      {id:1 name:'hello' address: '123 fake street'},
   ]
}

How would I be able to access only this value? I've tried console.log(this.state.list[1].name) console.log(this.state.list.name[1]) but to no avail.

Whenever I open up the browser console all I can see is undefined. Any help would be great

2
  • Please provide more related code, by now it's hard to tell where went wrong Commented Mar 25, 2020 at 21:08
  • js arrays are zero based. so this.state.list[0].name Commented Mar 25, 2020 at 21:19

1 Answer 1

2

Arrays in JavaScript do not start with one, they start with zero. It's known as "Zero base numbering" or "Zero base indexing" which means, the first index of an array should be accessed with 0, not 1.

state = {
   editingToggle: false,
   list : [
      {id:1 name:'hello' address: '123 fake street'},
   ]
}

console.log(this.state.list[0]); // {id:1 name:'hello' address: '123 fake street'
console.log(this.state.list[0].name); // 'hello'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help!

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.