0

I have a JSON node called 'data' that has an id. I want to store the value of that id but I'm unable to do so with my current syntax and I'm getting an error saying it's undefined.

How should I change my code to make this work?

My code snippet:

const transfer_ID = data.items.id;
    console.log("This is the transfer tree obtained", data);

data:

{ entity: 'collection',
  count: 1,
  items: 
   [ { id: 'trf_Ary1cWasdfGmHAc',
       entity: 'transfer',
       source: 'pay_ASrxasdfetwhAFv',
       amount: 390000,
       currency: 'INR',
       amount_reversed: 0,
       notes: [],
       fees: 1151,
       tax: 176,
       on_hold: false,
       on_hold_until: null,
       recipient_settlement_id: null,
       created_at: 1530208843 } ] }
2
  • data.items[0].id; Commented Jun 28, 2018 at 18:08
  • Don't give up on your first try. If you had debugged with your log statement and inspect just what data.items gives you, you would've figure it out right away! Commented Jun 28, 2018 at 20:00

2 Answers 2

3

check following code snippet

var data ={ entity: 'collection',
  count: 1,
  items: 
   [ { id: 'trf_Ary1cWasdfGmHAc',
       entity: 'transfer',
       source: 'pay_ASrxasdfetwhAFv',
       amount: 390000,
       currency: 'INR',
       amount_reversed: 0,
       notes: [],
       fees: 1151,
       tax: 176,
       on_hold: false,
       on_hold_until: null,
       recipient_settlement_id: null,
       created_at: 1530208843 } ] } 
       
   var id =  data.items[0].id;
   console.log(id);

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot read the object as data.items.id because items is an array. You have to access the specific index that you wish to get the ID for.

Example: const transfer_ID = data.items[0].id;

You could get an array of ID's by iterating over the array and storing those ID's as needed.

Comments

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.