Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 反向滤波阵列_Arrays_Angular_Typescript_Reverse - Fatal编程技术网

Arrays 反向滤波阵列

Arrays 反向滤波阵列,arrays,angular,typescript,reverse,Arrays,Angular,Typescript,Reverse,我有一个像['a','b','c','d','e','f']这样的数组,我只想反转我之前过滤过的部分,例如我过滤得到['c','d','e'],然后我做了反转,我期望的最终结果是['a','b','e','d','c','f'] 我尝试过使用array.slice.filtercondition.reverse和stackoverflow的其他解决方案,但没有人为我工作 知道吗?如果知道要反转的子数组的起始索引和长度 常数arr=['a','b','c','d','e','f']; 功能反转耳

我有一个像['a','b','c','d','e','f']这样的数组,我只想反转我之前过滤过的部分,例如我过滤得到['c','d','e'],然后我做了反转,我期望的最终结果是['a','b','e','d','c','f']

我尝试过使用array.slice.filtercondition.reverse和stackoverflow的其他解决方案,但没有人为我工作


知道吗?

如果知道要反转的子数组的起始索引和长度

常数arr=['a','b','c','d','e','f']; 功能反转耳环,i,j{
而如果知道要反转的子数组的起始索引和长度

常数arr=['a','b','c','d','e','f']; 功能反转耳环,i,j{
而问题的一个快速解决方案是获取已筛选的索引数组,并用反向值替换这些索引。请参见此示例:

常量数组=['a','b','c','d','e','f']; 常量indexHolder=[]; 常量filterValues=['c','d','e'] 常数过滤=数组 片 .filteritem,索引=>{ const shouldFilter=filterValues.includeItem; 如果应该过滤{ index.pushindex; } 返回滤波器; } 颠倒 const result=array.mapitem,索引=>{ const foundIndex=indexHolder.indexOfindex; 如果foundIndex>=0{ //找到了,所以我们需要反转 返回过滤后的[indexHolder.indexOfindex]; } 退货项目; };
console.logresult;您的问题的一个快速解决方案是获取已筛选的索引数组,并将这些索引替换为反向值。请参见此示例:

常量数组=['a','b','c','d','e','f']; 常量indexHolder=[]; 常量filterValues=['c','d','e'] 常数过滤=数组 片 .filteritem,索引=>{ const shouldFilter=filterValues.includeItem; 如果应该过滤{ index.pushindex; } 返回滤波器; } 颠倒 const result=array.mapitem,索引=>{ const foundIndex=indexHolder.indexOfindex; 如果foundIndex>=0{ //找到了,所以我们需要反转 返回过滤后的[indexHolder.indexOfindex]; } 退货项目; };
console.logresult;一个快速解决方案将在阵列中执行两次。一次清除所需的框,一次反向填充

变量x=['a','b','c','d','e','f']; 变量cond=['b','d','e']; var筛选=[]; 对于设i=0;i=0;i-{ 如果x[i]==null{ x[i]=过滤后的[计数器]; 计数器++; } }
console.logx;一个快速解决方案将在阵列中执行两次。一次清除所需的框,一次反向填充

变量x=['a','b','c','d','e','f']; 变量cond=['b','d','e']; var筛选=[]; 对于设i=0;i=0;i-{ 如果x[i]==null{ x[i]=过滤后的[计数器]; 计数器++; } }
console.logx;我的答案基本上是对Bas的模拟,如果您想使用函数,可以使用一些不同的格式。但是,所有的功劳都归于Bas,因为它为我的解决方案完成了腿部工作

ngOnInit() {
   const array = ['a', 'b', 'c', 'd', 'e', 'f'];
   const filterValues = ['c', 'd', 'e']

   console.log(this.resultFn(array, filterValues));
}

resultFn(array, filterValues) {
  const indexHolder = [];
  const filtered = this.filteredFn(array, filterValues, indexHolder);

  return array.map((item, index) => {
    const foundIndex = indexHolder.indexOf(index);
    if (foundIndex >= 0) {
      // found, so we need the reversed
      return filtered[indexHolder.indexOf(index)];
    }

    return item;
  });
}

filteredFn(array, filterValues, indexHolder) {
  return array
    .slice()
    .filter((item, index) => {
      const shouldFilter = filterValues.includes(item);

      if (shouldFilter) {
        indexHolder.push(index);
      }

      return shouldFilter;
    })
    .reverse();
}

我的答案基本上是模拟Bas,如果您想使用函数,可以使用一些不同的格式。但是所有的功劳都归于Bas,因为它为我的解决方案做了一些腿部工作

ngOnInit() {
   const array = ['a', 'b', 'c', 'd', 'e', 'f'];
   const filterValues = ['c', 'd', 'e']

   console.log(this.resultFn(array, filterValues));
}

resultFn(array, filterValues) {
  const indexHolder = [];
  const filtered = this.filteredFn(array, filterValues, indexHolder);

  return array.map((item, index) => {
    const foundIndex = indexHolder.indexOf(index);
    if (foundIndex >= 0) {
      // found, so we need the reversed
      return filtered[indexHolder.indexOf(index)];
    }

    return item;
  });
}

filteredFn(array, filterValues, indexHolder) {
  return array
    .slice()
    .filter((item, index) => {
      const shouldFilter = filterValues.includes(item);

      if (shouldFilter) {
        indexHolder.push(index);
      }

      return shouldFilter;
    })
    .reverse();
}

假设输入数组是['a'、'b'、'c'、'd'、'e'、'f'],您的过滤条件过滤掉['b'、'd'、'f'],您希望最终的输出是什么样的?您希望输出是[a,c,e,f,d,b]?假设输入数组是['a'、'b'、'c'、'd'、'e'、'f'],您的过滤条件过滤掉['b'、'd'、'f'],您希望最终的输出是什么样的?您希望输出是[a,c,e,f,d,b]吗?您能预测['a','b','c','d','e','c','f','d']的输出吗?通过您的函数?@Sheelpriy['a','b','d','c','e','d','f','c'],这是合乎逻辑的,尽管它可能不是你想要的,但f使它保持在这个位置,第一个要移动的字符是c,所以这将是最后一个。所以“e”不会失去它的位置?我不确定你在这里颠倒什么。@Sheelpriy 5个字母应该被移动/过滤:c,d,e,c,d当你把一个奇数长度的数组放在中间时,中间的一个就会出现在中间。你能预测输出的‘a’、‘b’、‘c’、‘d’、‘e’、‘c’、‘f’、‘d’吗?根据你的函数吗?@ SeelPyy [ a’,b’,d’,c’,e’,d’,f′,c′]。,这是合乎逻辑的,尽管它可能不是你想要的,但f使它保持在这个位置,第一个要移动的字符是c,所以这将是最后一个。所以“e”不会失去它的位置?我不确定你在这里颠倒什么。@Sheelpriy 5个字母应该被移动/过滤:c,d,e,c,d东南
是,你得到:d,c,e,d,c。这就是当你休息一个奇数长度的数组时会发生的事情,中间的一个将停留在中间。我喜欢你所想的方式,但是通过这样做,索引持有者会被一个事实填充:当你把数组传递给函数时,你把内存指针作为输入。我要做的是在filteredFn函数中返回一个数组,filteredFn在索引0处,indexHolder在索引1处,如图所示。如果愿意,也可以返回一个对象并使用给定的名称进行分解。我喜欢您的想法,但通过这样做,indexHolder被这样一个事实填充:当您将数组传递给函数时,您将内存指针作为输入。我要做的是在filteredFn函数中返回一个数组,filteredFn在索引0处,indexHolder在索引1处,如图所示。或者,如果愿意,您也可以返回一个对象并使用给定的名称进行分解。只有当您要选择的对象都分组在一起时,这才有效。此外,您还需要知道该组的开始索引和结束索引。只有当您要选择的内容全部分组在一起时,这才有效。此外,您还需要知道该组的开始索引和结束索引。