0

I am getting the following information from a parsed JSON file. How can I get the value of the [ARRAY] element from the last 'gfs' element,(or whatever it is called)? I have never worked on JSON, What all should I know to do this?

  data: [
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'base',
      pos: 0,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 1,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 2,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'boomerang',
      pos: 3,
      gfs: [Array]
    }
  ]

[ARRAY] holds some information, I need.

I got this by parsing a JSON string, and printing it to the console.

PS. Only using Javascript.

  • @PrabhjotSinghKainth I have used this to get the result I have showed above. –  Dec 02 '19 at 06:52

4 Answers4

2

Basically, you are getting an Array of Object and you want to access the last element of the array, you can get last array position by array.length - 1, and access the gfs value. if you want to check whether gfs value is array not then you can check by typeof gfs

var  data= [
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'base',
          pos: 0,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 1,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 2,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'boomerang',
          pos: 3,
          gfs: ['a','b','c']
        }
      ]
    
    // You can access like this
    console.log(data[data.length-1].gfs)
Narendra Chouhan
  • 2,291
  • 1
  • 15
  • 24
0

You can access the array value by index so you can use loop to access it by index or you can directly access it using index number.

Check out these docs. It will help out to better understand

Loops_and_iteration

Array

Property accessors

let data =  [
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'base',
      pos: 0,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 1,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 2,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'boomerang',
      pos: 3,
      gfs: [1, 2, 4, 5]
    }
  ];
// by looping 
for(let i = 0; i < data.length; i++) {   // iterate on data, for(initilize, condition, increment)
 let item = data[i];                 // access item from data by bracket notation property accessors, pass index number i
 let gfsList = item['gfs']        // grab the gfs object from item by bracket notation property accessors
 if(Array.isArray(gfsList)) {      // check if gfs is array or not
  for(let j = 0; j < gfsList.length; j++) {   //gfs is array so we can iterate again on each item of it
   let gfsItem = gfsList[j];
   console.log(gfsItem);
  }
 }
}
// direct access 
console.log(data[data.length - 1]['gfs'])
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20
0

JSON.parse() gives JavaScript object. Here it is the Array object, so you can access it by looping the array or specifying the array index directly.

var  data = [
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'base',
          pos: 0,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 1,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 2,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'boomerang',
          pos: 3,
          gfs: [Array]
        }
      ]

    data.forEach(data => {
      console.log(data.gfs); //Print the gfs value of every object in the array
    })
Ininiv
  • 1,325
  • 7
  • 12
0

Accessed it by using console.log(obj.data[3].gfs)

I didn't take into account that the data key stored inside data inside obj(which I assigned)/