Javascript 如何删除所有向左或向右移动的数据

Javascript 如何删除所有向左或向右移动的数据,javascript,typescript,Javascript,Typescript,我这里有一个数据是: data = ['one', 'two', 'three', 'four', 'five', 'six']; value = 'three'; 如何将所有起始“四个”删除为“大小” 预期产出: data = ['one', 'two', 'three']; data = ['three', 'four', 'five', 'six']; 删除向左移动的数据时 以下是预期输出: data = ['one', 'two', 'three']; data = ['thre

我这里有一个数据是:

data = ['one', 'two', 'three', 'four', 'five', 'six'];
value = 'three';
如何将所有起始“四个”删除为“大小”

预期产出:

data = ['one', 'two', 'three'];
data = ['three', 'four', 'five', 'six'];
删除向左移动的数据时

以下是预期输出:

data = ['one', 'two', 'three'];
data = ['three', 'four', 'five', 'six'];
删除右边的数据时。

应该能够执行您想要的操作。要知道在何处进行切片,首先需要找到
three
的索引,如果数组更复杂,可以使用或来完成

找到索引后:

  • 要删除左侧的项目,只需执行
    data.slice(index)

    这意味着将数组从索引一直切片到数组末尾。您不需要提供第二个参数,因为隐式假设为数组的长度
  • 要删除右侧的项目,可以执行
    data.slice(0,索引+1)

    这意味着从开始到索引再加上一(加上一,因为您希望包含
    three
    条目)
见下面的概念证明:

const data=['1','2','3','4','5','6'];
常量值='3';
功能移除(arr,val){
常数idx=arr.indexOf(val);
返回arr.slice(idx);
}
功能拆卸灯(arr、val){
常数idx=arr.indexOf(val);
返回arr.slice(0,idx+1);
}
log(removeLeft(数据、值));
日志(数据、值)您可以结合使用,如下所示:

const data=['1','2','3','4','5','6'];
const index=data.indexOf('three');
const leftResult=data.slice(0,索引+1);
const rightResult=data.slice(索引);
console.log(leftResult);
console.log(rightResult)检查此项:

data = data.slice(data.indexOf(value));
以及:


您有两个
预期输出
。哪一个是正确的?要保留相同的对象引用吗?