9

I am trying to get the first object from an array of objects. Fetched data in componentDidMount and return:

var array1 = [
    {
      Id: 1, 
      Categories: 200 
    },
    {
      Id: 2, 
      Categories: 100 
    }
]

(real data is much more complicated but in the same format) What I want is this:

array2 = [
    Id: 1, 
    Categories: 200
]

I was using

var iterator1 = array1.entries();
let array2 = iterator1.next().value

and when I console.log(array2) it shows the array data (all key and value is in index 1 of array), then when I console.log(array2[1].Id) it shows undefined right inside the render method in class component. I use the same code in other IDE but not using react the code work. Could someone explain to me please? Appreciate for any help.

Edit: At the end I found that I asked the wrong question. I failed to get the data because I declared the data in my reducer to object instead of array. I mark the answer by larz since you just need to put index 0 to get the first object from the array of objects.

andromad
  • 143
  • 1
  • 2
  • 8

1 Answers1

14

Grab the first item in your array using [0], which returns an object in your case. Then you can call .Id on it to get the id.

var array1 = [
    {
      Id: 1, 
      Categories: 200 
    },
    {
      Id: 2, 
      Categories: 100 
    }
];

var first = array1[0];

console.log(first, first.Id);
larz
  • 5,724
  • 2
  • 11
  • 20
  • In Javascript, the first item in an array is always 0, not 1. It can be useful for certain things, but it can also be slightly annoying when you first start using the language if you're not used to it (I came from Lua.) – Gage Hendy Ya Boy Aug 23 '18 at 16:23
  • No. array2 holds `[0, {Id:1, Categories: 200}]`. Thus, OP is doing right. – Bhojendra Rauniyar Aug 23 '18 at 16:24
  • @BhojendraRauniyar Well I didn't mention anything about `array2` so I'm not sure why you commented. – larz Aug 23 '18 at 16:26
  • Try OP code, then you'll understand. – Bhojendra Rauniyar Aug 23 '18 at 16:27
  • @BhojendraRauniyar Then comment on OP's code, not mine. There is no `array2` in mine because that's an unnecessary step. – larz Aug 23 '18 at 16:27
  • What are you doing here? You're trying to help OP by correcting OP's mistake. But I din't find any issue with the OP code. So, your answer is invalid. – Bhojendra Rauniyar Aug 23 '18 at 16:29
  • i use array1[0] show undefined but array1[1] showed all the data in console. But when I use array1[1].anykey , undefined. All this code is inside the render method of react component class and data came from fetch method in action. – andromad Aug 23 '18 at 16:29
  • @andromad that makes me think what what you posted as `array1` is incorrect. Can you `console.log(array1)` and double check that you didn't miss anything? – larz Aug 23 '18 at 16:30
  • console.log(array1) return the original array of objects, all data is inside. And I feel like doing all this code inside the render method is so unstable. – andromad Aug 23 '18 at 16:33
  • @andromad well if you run my code snippet, you can see that `array1[0]` does not return `undefined` and `array1` was copied directly from your question. Help me help you. – larz Aug 23 '18 at 16:35