1

I have a JSON file with these values :

{ 
  id:
  name:
  lastName:
  Info:[ 
    {
      info1:
      info2:
      info3: 
    },
    { 
      info1:
      info2:
      info3:
    }
  ]
}

So, Info object has two lists. I want to be able to access the last one. data.info[0] is for the first object. How do I access the last one ? (var data holds the JSON results). In my Entity I have a Client entity that has a one to many mapping with InfoData,as a list : Like this :

    @OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
    @JsonManagedReference  
    private List<InfoData> info= new ArrayList<InfoData>();

Thank you.

Héctor
  • 24,444
  • 35
  • 132
  • 243
  • Possible duplicate of [get last element of a json object in javascript](https://stackoverflow.com/questions/14148065/get-last-element-of-a-json-object-in-javascript) – Jonathan Oct 11 '17 at 09:52

2 Answers2

0

Do this:

    var length = data.Info.length; // number of objects in the array
    var requiredObject = {};

    requiredObject = data.Info[length-1]; //get the last object
    console.log(requiredObject);
Harman
  • 298
  • 1
  • 13
0

Here's an example:

var obj = {
name: "test",
Info: [{
       info1: "a",
       info2: "b"
      },{
       info1: "a",
       info2: "b"
       }]
      }

       var lastObject = obj.Info[obj.Info.length - 1];    
JP Dolocanog
  • 451
  • 3
  • 19