-1

I have been stuck on this for a while. I have an array of object where the key is the same for all of the objects (see below). I am trying to extract the data from the first object in the array [0]. However when I console.log the data I get 'Cannot read property '0' of undefined. Here is the image of the data that I receive when I just filter the data.enter image description here

And here is the code that I am using to get that data:

const handleClick = e => {
    e.preventDefault();
    const buttonValue = e.target.value;
    console.log(buttonValue);
    grid.on('rowClick', (...args) =>
      args.filter(data => {
        data.cells;
        console.log(data.cells);
      })
    );
  };

when I add an index of 0 to the console.log console.log(data.cells[0]); I get the cannot read property 0 of undefined

timbo245
  • 175
  • 1
  • 14
  • 1
    Where do 'cells' come from? The error is stating that it's undefined. – olawrdhalpme Mar 18 '21 at 17:55
  • That little blue `[i]` is saying your data is loading asynchronously... `console.log(JSON.stringify(data.cells));` https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays So you ordered a pizza and you try to eat it before it was delivered to your house. – epascarello Mar 18 '21 at 17:57
  • Can you provide the result of 'console.log(data)' ? – sanjar Mar 18 '21 at 18:01

1 Answers1

0

I am not sure where data.cells is coming from. Property 0 of undefined clearly tells you that there is no such property as cells on data.

Also, I am not sure what exactly you mean by extracting objects, but I will be assuming that you need programmatic access to the objects.

First off, if you can see that the statement (8)[ n, n, n, n, n, n, n, n ] tells you that it is an object with 8 objects of type n. Here is something that I simulated in the console.

Here is something I simulated in a console. So you can clear that up.

And to access it you need to use array indexing. Let's assume the name of the array is data -

const firstElement = data[0]

You can also use loops and higher-order functions such as map(), forEach() too.