[英]How should I slice the array of objects of objects
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)我正在前端实现一个分页,其中我将数组中每个 object 中的每个对象切片为给定值,这样如果切片 (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.意味着只有 2 个对象应该被切掉。 my function is as following but its not working.
我的 function 如下,但它不起作用。
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.这里 start 和 end 分别表示页面中开始 object 的索引和页面中最后 object 的索引。 Can anyone help me with the code?
谁能帮我写代码? I'll be very grateful.
我将不胜感激。
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; }
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.
这是因为本质上 object 的 id 是关键。 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;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.