Home > front end >  How should I slice the array of objects of objects
How should I slice the array of objects of objects

Time:11-18

i have a array which looks like this :

[

{
    "1": {
        "id": 1,
        "price": 569.2,
        "marketcap": 94943213384.68,
        "timestamp": 1637136047,
        "datetime": "2021-11-17 08:00:47"
    },
    "2": {
        "id": 2,
        "price": 59745.7,
        "marketcap": 1127634159487.02,
        "timestamp": 1637136064,
        "datetime": "2021-11-17 08:01:04"
    },
    "3": {
        "id": 3,
        "price": 592.53,
        "marketcap": 11199494644.95126,
        "timestamp": 1637136053,
        "datetime": "2021-11-17 08:00:53"
    }
},
{
    "1": {
        "id": 1,
        "last30dcommits": null,
        "activity": "No Github Repo Found"
    },
    "2": {
        "id": 2,
        "last30dcommits": 109,
        "activity": "High Activity"
    },
    "3": {
        "id": 3,
        "last30dcommits": 44,
        "activity": "Medium Activity"
    }
},
{
    "1": {
        "id": 1,
        "change24h": -6.75,
        "change7d": -10.18,
        "change30d": 17.32,
        "timestamp24h": 1637035200,
        "timestamp7d": 1636516800,
        "timestamp30d": 1634529600
    },
    "2": {
        "id": 2,
        "change24h": -3.22,
        "change7d": -12.58,
        "change30d": -5.17,
        "timestamp24h": 1637035200,
        "timestamp7d": 1636516800,
        "timestamp30d": 1634529600
    },
    "3": {
        "id": 3,
        "change24h": -6.37,
        "change7d": -17.81,
        "change30d": -5.04,
        "timestamp24h": 1637035200,
        "timestamp7d": 1636516800,
        "timestamp30d": 1634529600
    }
},
{
    "1": {
        "tweetsRating": 44.04,
        "newsRating": 38.42
    },
    "2": {
        "tweetsRating": 14.76,
        "newsRating": -1.27
    },
    "3": {
        "tweetsRating": 0,
        "newsRating": 44.35
    } 
},
]

I am implementing a pagination in the front end in which I am slicing each objects in each object in the array to a given value so that I get a result something like this if I slice (0,2)

[

{
    "1": {
        "id": 1,
        "price": 569.2,
        "marketcap": 94943213384.68,
        "timestamp": 1637136047,
        "datetime": "2021-11-17 08:00:47"
    },
    "2": {
        "id": 2,
        "price": 59745.7,
        "marketcap": 1127634159487.02,
        "timestamp": 1637136064,
        "datetime": "2021-11-17 08:01:04"
    }
},
{
    "1": {
        "id": 1,
        "last30dcommits": null,
        "activity": "No Github Repo Found"
    },
    "2": {
        "id": 2,
        "last30dcommits": 109,
        "activity": "High Activity"
    }
},
{
    "1": {
        "id": 1,
        "change24h": -6.75,
        "change7d": -10.18,
        "change30d": 17.32,
        "timestamp24h": 1637035200,
        "timestamp7d": 1636516800,
        "timestamp30d": 1634529600
    },
    "2": {
        "id": 2,
        "change24h": -3.22,
        "change7d": -12.58,
        "change30d": -5.17,
        "timestamp24h": 1637035200,
        "timestamp7d": 1636516800,
        "timestamp30d": 1634529600
    }
},
{
    "1": {
        "tweetsRating": 44.04,
        "newsRating": 38.42
    },
    "2": {
        "tweetsRating": 14.76,
        "newsRating": -1.27
    }
},
]

means only 2 objects should be sliced out. my function is as following but its not working.

const sliceData = (array,start,end)=>{
    array.forEach(arr=>{
       Object.keys(arr).slice(start - 1, end   1).reduce((result, key) => {
          result[key] = arr[key];

      return result;
    }
})
}

Here start and end represents the index of starting object in a page and index of last object in the page respectively. Can anyone help me with the code? I'll be very grateful.

CodePudding user response:

You could slice the entries for new objects.

const
    data = [{ "1": { id: 1, price: 569.2, marketcap: 94943213384.68, timestamp: 1637136047, datetime: "2021-11-17 08:00:47" }, "2": { id: 2, price: 59745.7, marketcap: 1127634159487.02, timestamp: 1637136064, datetime: "2021-11-17 08:01:04" }, "3": { id: 3, price: 592.53, marketcap: 11199494644.95126, timestamp: 1637136053, datetime: "2021-11-17 08:00:53" } }, { "1": { id: 1, last30dcommits: null, activity: "No Github Repo Found" }, "2": { id: 2, last30dcommits: 109, activity: "High Activity" }, "3": { id: 3, last30dcommits: 44, activity: "Medium Activity" } }, { "1": { id: 1, change24h: -6.75, change7d: -10.18, change30d: 17.32, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "2": { id: 2, change24h: -3.22, change7d: -12.58, change30d: -5.17, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "3": { id: 3, change24h: -6.37, change7d: -17.81, change30d: -5.04, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 } }, { "1": { tweetsRating: 44.04, newsRating: 38.42 }, "2": { tweetsRating: 14.76, newsRating: -1.27 }, "3": { tweetsRating: 0, newsRating: 44.35 } }],
    result = data.map(o => Object.fromEntries(Object.entries(o).slice(0, 2)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

I really think the data format/structure sent from BackEnd should be changed. Instead of sending Array of objects you should consider sending Array of Array of objects from Backend. This is because essentially the id of the object is key. However, Here is how you can slice the data if you do not have permission to change data sent from Backend.

const sliceData = (array,start,end)=>{ 
    return array.map(el => {
        let newObj = Object.values(el).filter(x => x.id>start && x.id<=end).reduce((acc,x) => {
          acc[x.id] = x;
          return acc;  
        } ,{}) 
        return newObj;
    });
}
  • Related