英文:
Return last 10 objects of array in typescript
问题
我有一个数组:
myArr = [
{"name": "John", "schedule": [{"course": "ESL", "hours": 25},{"course": "Math", "hours": 50},{"course": "History", "hours": 75}]},
{"name": "Julia", "schedule": [{"course": "English", "hours": 20},{"course": "Geography", "hours": 35},{"course": "Math", "hours": 55}]},
{"name": "Adam", "schedule": [{"course": "Physics", "hours": 15},{"course": "Math", "hours": 50},{"course": "Chemistry", "hours": 60}]}
];
如果myArr的长度大于2或小于10,则需要返回未更改的myArr,但如果长度超过10,则需要返回数组的最后15个对象。
我写了一个函数,但数组返回为空。
export const slicedData = (schedule: Schedule[], name: string) => {
const result: Schedule[] = [];
result.push({name, schedule});
if(result.length >= 2 && result.length <= 10){
return result;
}
return result.slice(-9);
}
请告诉我我漏掉了什么?
英文:
I have an array:
myArr= [
{"name":"John", "schedule": [{"course":"ESL", "hours": 25},{"course": "Math", "hours": 50},{"course": "History", "hours": 75}]},
{"name":"Julia", "schedule": [{"course":"English", "hours": 20},{"course": "Geography", "hours": 35},{"course": "Math", "hours": 55}]},
{"name":"Adam", "schedule": [{"course":"Physics", "hours": 15},{"course": "Math", "hours": 50},{"course": "Chemistry", "hours": 60}]}
];
if the length of myArr is more than 2 or less than 10, need return myArr unchanged, but if it more than 10 then need to return last 15 objects of array.
I wrote function, but array returns empty.
export const slicedData = (schedule:Schedule[], name:string) =>
{
const result:Schedule[] = [];
result.push({name, schedule});
if(result.length>=2 && result.length<=10){
return result;
}
return result.slice(-9);
}
please advise what i am missing?
答案1
得分: 0
如@jsejcksn在评论中所写,你应该使用AND(&&)操作符而不是OR(||)。这可以保证值在2和10之间。
再次,如@Martijn所指出,你正在创建一个只有一个值的数组。如果你已经有了myArr
,你应该将它传递给函数并使用它。
export const sliceData = (dataToSlice: []) => {
if (dataToSlice.length >= 2 && dataToSlice.length <= 10) {
return dataToSlice; // 返回未改变的调度
}
// 否则返回调度的最后10个元素
return dataToSlice.slice(-10);
}
如果你没有这个数组,创建一个函数首先创建你需要的数组,然后调用sliceData
是一个好主意。
一个小的改进是传递要返回的最后元素的数量:
export const sliceData = (dataToSlice: Schedule[], fromLastElement: number) => {
if (dataToSlice.length >= 2 && dataToSlice.length <= 10) {
return schedule; // 返回未改变的调度
}
// 否则返回调度的最后n个元素
return dataToSlice.slice(-fromLastElement);
}
英文:
As @jsejcksn wrote in comment, you should use AND (&&) operator instead of OR (||). This guarantee that the value is between 2 and 10.
Again, as @Martijn noticed, you are creating an array with just one value. If you already have myArr
you should pass it to function and use it.
export const sliceData = (dataToSlice: []) =>
{
if(dataToSlice.length>=2 && dataToSlice.length<=10){
return dataToSlice; // return schedule unchanged
}
// Otherwise return last 10 element of schedule
return dataToSlice.slice(-10);
}
If you don't have the array, is a good idea to create a function that first create your desired array and the call sliceData
.
A little improvement could be to pass the number of last elements to return:
export const sliceData = (dataToSlice: Schedule[], fromLastElement: number) =>
{
if(dataToSlice.length>=2 && dataToSlice.length<=10){
return schedule; // return schedule unchanged
}
// Otherwise return last n elements of schedule
return dataToSlice.slice(-fromLastElement);
}
答案2
得分: 0
I guess the issue is you are pushing a single object into the result
and waiting for the whole myArr
to be returned. Also, you are checking the result.length
instead of the myArr
length when it comes to pushing. And if you want to have the last 15 objects of the array, you need to use -15
instead of -9
. With these changes, the code should be like below.
export const slicedData = (myArr: any[]) => {
if (myArr.length >= 2 && myArr.length <= 10) {
return myArr;
}
return result.slice(-15);
}
英文:
I guess the issue is you are pushing a single object into the result
and waiting for whole myArr
to be returned. Also you are checking the result.length
instead of the myArr
length when comes to pushing. And if you want to have the last 15 objects of array you need to use -15
instead of -9
. With these changes code should be like below.
export const slicedData = (myArr:any[]) =>
{
if(myArr.length>=2 && myArr.length<=10){
return myArr;
}
return result.slice(-15);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论