返回 TypeScript 数组的最后 10 个对象。

huangapple go评论54阅读模式
英文:

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= [
  {&quot;name&quot;:&quot;John&quot;, &quot;schedule&quot;: [{&quot;course&quot;:&quot;ESL&quot;, &quot;hours&quot;: 25},{&quot;course&quot;: &quot;Math&quot;, &quot;hours&quot;: 50},{&quot;course&quot;: &quot;History&quot;, &quot;hours&quot;: 75}]},
  {&quot;name&quot;:&quot;Julia&quot;, &quot;schedule&quot;: [{&quot;course&quot;:&quot;English&quot;, &quot;hours&quot;: 20},{&quot;course&quot;: &quot;Geography&quot;, &quot;hours&quot;: 35},{&quot;course&quot;: &quot;Math&quot;, &quot;hours&quot;: 55}]},
  {&quot;name&quot;:&quot;Adam&quot;, &quot;schedule&quot;: [{&quot;course&quot;:&quot;Physics&quot;, &quot;hours&quot;: 15},{&quot;course&quot;: &quot;Math&quot;, &quot;hours&quot;: 50},{&quot;course&quot;: &quot;Chemistry&quot;, &quot;hours&quot;: 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) =&gt;
{
  const result:Schedule[] = [];
  result.push({name, schedule});

  if(result.length&gt;=2 &amp;&amp; result.length&lt;=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: []) =&gt;
{
if(dataToSlice.length&gt;=2 &amp;&amp; dataToSlice.length&lt;=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) =&gt;
{
if(dataToSlice.length&gt;=2 &amp;&amp; dataToSlice.length&lt;=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[]) =&gt;
{
if(myArr.length&gt;=2 &amp;&amp; myArr.length&lt;=10){
return myArr;
}
return result.slice(-15);
}

huangapple
  • 本文由 发表于 2023年5月31日 23:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定