1

I don't get how to get data from an array filled with objects in Javascript. Here is my code and error message :

//First I create the table
var dataset = [];

//Then I declare the object
var PersReunionObj = {};

//Now I throw some data into the object
PersReunionObj.fk_idPers = fk_idPers;
PersReunionObj.fk_idReunion = fk_idReunion;
PersReunionObj.isPresent = isPresent;

//I insert the object into a table    
dataset[0] = PersReunionObj;

Now I would like to get back those data from my table

console.log(dataset[0]);

Gives me :

{fk_idPers: 1, fk_idReunion: 1, isPresent: true}

So my table is OK. But then to get data, i tried something like

console.log(dataset[0].PersReunionObj.fk_idPers);

And it gives me an error :

Uncaught TypeError: Cannot read property 'fk_idPers' of undefined

How can i fix that ?

3
  • 1
    dataset[0] = PersReunionObj so dataset[0] is the PersReunionObj. Do dataset[0].fk_idPers Commented Jun 15, 2018 at 2:16
  • 1
    You might want to read about how to Access / process (nested) objects, arrays or JSON. Commented Jun 15, 2018 at 2:34
  • Yes thanks, i'll have a look at that :) Commented Jun 15, 2018 at 2:54

1 Answer 1

3

You simply need to call because dataset[0] is PersReunionObj

console.log(dataset[0].fk_idPers);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thanks, it's right it works.I feel a bit stupid right now :/
@Ishiru- No worries. When learning everyone ought to think and ask :)

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.