# How to get all index value?

``````contract Bar{

struct Foo{
uint x;
}
mapping(uint => Foo[]) foo;

function add(uint id, uint _x) public {
foo[id].push(Foo(_x));
}

function get(uint id, uint index) public returns(uint){
return foo[id][index].x;
}
}
``````

I'm trying to call all the index value. Like there will be return function which will return all the total index value.

And If suppose I wish to return the last 10 data of address ID + Index value. How would I call that.

Because I need to enter the index value, but if someone do not know the index value. Then it will be difficult.

To get the last ten values you could add this function:

``````function get_last_ten(uint id) public returns(uint){
uint memory lastItems;
for(uint i=0;i<10;i++){
if(foo[id].length>i){
lastItems[i] = foo[id][foo[id].length-i-1].x;
}
}
return lastItems;
}
``````

This will return an array of ten values the first value is the last element in the specific address `id` of `foo`.

hope this helps

• Thanks a lot. This really worked. Thankyou #Jaime for your help. – shrey bhardwaj Aug 13 '18 at 9:13

You would derive a different solution, but I would suggest you 2 different ways that I would prefer mostly.

``````pragma solidity ^0.4.24;

contract StackExchangeSol{

struct Foo{
uint x;
}

mapping(uint => Foo[]) foo;
//Solution-2
uint[] indexOf;
//Solution-2

function add(uint id, uint _x) public {
foo[id].push(Foo(_x));
//Solution-1
count++;
//Solution-1
}

function get(uint id, uint index) public view returns(uint){
return foo[id][index].x;
}
//Solution-1
uint count;
constructor() public{
count=0;
}
//Solution-1

//Solution-2
function getLenOf() public view returns(uint){
return indexOf.length;

}
//Solution-2
}
``````
• But it is not returning any value. – shrey bhardwaj Aug 13 '18 at 6:45