javascript - Angular - 如何根据某些特定检查来减少数组?

标签 javascript arrays angular

我有一个从数据库获得的有序数组,看起来像:

     let myArray = [      
        {month: 1, visible: true},
        {month: 2, visible: false}, 
        {month: 3, visible: true}, 
        {month: 4, visible: false},
        {month: 5, visible: false}, 
        {month: 6, visible: true},
        {month: 7, visible: true},
        {month: 8, visible: false}, 
        {month: 9, visible: true}, 
        {month: 10, visible: false},
        {month: 11, visible: false},
        {month: 12, visible: false}
      ];

我需要做的是将其减少到一个新数组,其中第一个对象将是具有属性 visible = true 的第一个 month ,然后它将花费最后一个具有 month = true 的对象的所有月份

这意味着只有当月份不按顺序排列(因此不会产生间隙)时,它才会忽略月份。

在上面的示例中,它将返回最后一个 3 (10, 11, 12) 旁边的所有对象,因为它们位于由第一个 TRUE 创建的序列之外。 code> 和最后一个 TRUE(在本例中为 1 月 9 月 )

我希望我已经说清楚了,那么我怎样才能完成我的reduce()呢?

myArray = myArray
    .sort((a, b) => a.month - b.month)
    .reduce((arr, current, idx) => {

      return arr;
    }, []);            

期望的输出:

 let myArray = [      
            {month: 1, visible: true},
            {month: 2, visible: false}, 
            {month: 3, visible: true}, 
            {month: 4, visible: false},
            {month: 5, visible: false}, 
            {month: 6, visible: true},
            {month: 7, visible: true},
            {month: 8, visible: false}, 
            {month: 9, visible: true}
];

最佳答案

找到起始位置很简单,但找到结束位置就有点棘手了。

我会采取这种方法:

  1. 查找第一个符合条件的索引
  2. 查找符合条件的最后一个索引
  3. 返回从第一个到最后一个的切片
// accept an array of type T and a predicate
// the predicate indicates which items define the inner range
// the predicate is a callback - a function - that accepts an argument of type T and returns a boolean

private filterInner<T>(arr: T[], predicate: (t: T) => boolean): T[] {

  // use findIndex to find the first matching index for the predicate

  const first = arr.findIndex(predicate);
  if (first === -1) {
    // no matching items in array
    return [];
  }


  // loop backwards to find the last matching index for the predicate

  let last = first;
  for (let i = arr.length - 1; i >= first; i--) {
    if (predicate(arr[i]) === true) {
      last = i;
      break;
    }
  }


  // return the portion of the range between the two indexes (inclusive)
  return arr.slice(first, last + 1);
}

就您而言,您可以像这样使用它:

myArray = myArray.sort((a, b) => a.month - b.month);
const filtered = this  
  .filterInner(myArray, x => x.visible);

演示:https://stackblitz.com/edit/angular-xrpsct

补充阅读

如果其中一些技术对您来说是新的,请阅读一些额外的内容:

Javascript 等价

这是没有 Typescript 噪音的纯 JavaScript 等效项。

function filterContiguous(arr, predicate) {
  const first = arr.findIndex(predicate);
  if (first === -1) {
    // no matching items in array
    return [];
  }

  let last = first;
  for (let i = arr.length - 1; i >= first; i--) {
    if (predicate(arr[i]) === true) {
      last = i;
      break;
    }
  }

  return arr.slice(first, last + 1);
}

关于javascript - Angular - 如何根据某些特定检查来减少数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60340185/

相关文章:

Java数组值被放入错误的数组中

angular - 如何使用 ngx-translate 的 TranslatePipe 测试组件?类型错误 : Cannot read properties of undefined (reading 'subscribe' )

javascript - 我应该使用一个组件来使用 ngif 进行编辑和查看,还是将它们分成多个组件?

包含二维数组类型的 C 结构定义

c++ - 总线错误 :10 with more indexes to go

javascript - 强制将 heroku/Angular 应用程序重定向到 HTTPS 版本

angular - p-dropdown 如何根据 ng-invalid 和 focus 选项更改边框颜色和阴影

javascript - 为什么状态设置正确后却没有声明?

javascript - 移动网站中带有图像和文本的按钮的最佳实践

javascript - 有什么方法可以滚动溢出 :hidden 的 div