简体   繁体   English

如何正确使用 splice 和 slice [Typescript]

[英]How to use splice and slice correctly [Typescript]

I'm new to typescript .我是typescript新手。 I'm sorry for asking such basic question.我很抱歉问这么基本的问题。 But I'm not able to figure out the problem in my code.但是我无法找出代码中的问题。 I've read many articles about slice and splice .我读过很多关于slicesplice文章。 I wanted to use them in my angular project(not both of them together).我想在我的 angular 项目中使用它们(不是同时使用它们)。 I'm getting unexpected outputs.我得到了意想不到的输出。 Here is my code:这是我的代码:

When using slice:使用切片时:

newArray:string[];
mutatedArray:string[];

removeOneFruit() {
  this.newArray=['Apple', 'Orange', 'Plums', 'Grapes'];
  console.log("Before slicing: "+this.newArray);
  this.mutatedArray=this.newArray.slice(this.newArray.indexOf('Orange'),1);
  console.log("After slicing: "+this.mutatedArray);
}

Output:输出:

Before slicing: Apple,Orange,Plums,Grapes切片前:苹果、橙子、李子、葡萄

After slicing:切片后:

The After slicing thing is simply blank.切片后的东西只是空白。 No errors or warnings on console.控制台上没有错误或警告。 This is strange for me.这对我来说很奇怪。

When using splice:使用拼接时:

newArray:string[];
mutatedArray:string[];

removeOneFruit() {
  this.newArray=['Apple', 'Orange', 'Plums', 'Grapes'];
  console.log("Before splicing: "+this.newArray);
  this.mutatedArray=this.newArray.splice(this.newArray.indexOf('Orange'),1);
  console.log("After splicing: "+this.mutatedArray);
}

Output:输出:

Before slicing: Apple,Orange,Plums,Grapes切片前:苹果、橙子、李子、葡萄

After slicing: Orange切片后:橙色

I don't understand what's happening.我不明白发生了什么。 I'm expecting an array with all the fruits but Orange .我期待一个包含除Orange所有水果的数组。 Please correct me.请纠正我。

PS: Its a miniature model of a huge project where things are not fruits and they ain't hard coded also. PS:它是一个大型项目的微型模型,其中的东西不是水果,也不是硬编码的。

.splice() returns the deleted elements, but if you use it in a separate line: .splice()返回已删除的元素,但如果您在单独的行中使用它:

 var arr = ['Apple', 'Orange', 'Plums', 'Grapes']; arr.splice(arr.indexOf('Orange'), 1); console.log(arr);

Or you can use .slice() like this: (it's a bit long)或者你可以像这样使用.slice() :(有点长)

 var arr = ['Apple', 'Orange', 'Plums', 'Grapes']; //This slices from the start up to "Orange", then concatenates from after "Orange" to the end of the array console.log(arr.slice(0, arr.indexOf('Orange')).concat(arr.slice( arr.indexOf('Orange') + 1, arr.length)));

So slice does not work the same as splice所以 slice 与 splice 的工作方式不同

Array.slice takes two parameter, start and end . Array.slice有两个参数, startend So in your first function you are giving the start index of Orange and the end index of 1 which I think doesn't make sense because the slice is getting items within a range, so there are no items between that range.因此,在您的第一个函数中,您提供了 Orange 的开始索引和 1 的结束索引,我认为这没有意义,因为切片正在获取某个范围内的项目,因此该范围之间没有项目。

So if you look at the code snippet I have the index of Orange and then the index one up because the slice is inclusive so you in your example Orange is at the 1 index and then you are doing 1 as the end index.因此,如果您查看代码片段,我有 Orange 的索引,然后索引为 1,因为切片是包含性的,因此在您的示例中,Orange 位于 1 索引处,然后您将 1 作为结束索引。 So I am pulling everything between index 1 and index 2, which is Orange.所以我将索引 1 和索引 2 之间的所有内容都拉出来,即橙色。

 let newArray; let mutatedArray; function removeOneFruit() { newArray=['Apple', 'Orange', 'Plums', 'Grapes']; console.log("Before slicing: "+newArray); mutatedArray=newArray.slice(newArray.indexOf('Orange'), newArray.indexOf('Orange')+1); console.log("After slicing: "+mutatedArray); } removeOneFruit()

Your second function you are using splice which is used to remove items from an array.您正在使用的第二个函数splice用于从数组中删除项目。 Array.splice takes an index and the amount of items you want to remove at that index . Array.splice接受一个索引以及您要在该索引处删除的项目数量 So all you are doing is creating a new array with the item that you remove.所以你要做的就是用你删除的项目创建一个新数组。 If you wanted return an array with everything but Orange.如果你想返回一个除了橙色以外的所有内容的数组。 Then you would run splice , which would remove Orange, and then point the mutatedArray to the new value of newArray.然后您将运行splice ,这将删除 Orange,然后将 mutatedArray 指向 newArray 的新值。

 let newArray; let mutatedArray; function removeOneFruit() { newArray=['Apple', 'Orange', 'Plums', 'Grapes']; console.log("Before splicing: "+newArray); newArray.splice(newArray.indexOf('Orange'),1); mutatedArray= newArray; console.log("After splicing: "+mutatedArray); } removeOneFruit()

Slice is not returning anything because the second parameter is the end index. Slice 没有返回任何内容,因为第二个参数是结束索引。 Since Orange is 1 and the end is 1 it will not return anything.因为 Orange 是 1 而 end 是 1 它不会返回任何东西。

Splice - will return the deleted items the original array is the one that gets affected. Splice - 将返回已删除的项目,原始数组是受影响的项目。

Read the docs !阅读文档

newArray.slice(newArray.indexOf('Orange'), 1);

This cuts a segment of an array from index newArray.indexOf('Orange') (which is 1 ) up to but not including index 1 .这从索引newArray.indexOf('Orange') (它是1 )到但不包括索引1剪切了一段数组。 This ends up as an empty array.这最终是一个空数组。

If you wanted a slice of length 2 from a given position, you would do:如果您想从给定位置获得长度为 2 的切片,您可以这样做:

var a = [100, 200, 300, 400];
var pos = a.indexOf(200);
a.slice(pos, pos + 2);  // == [ 200, 300 ]

If what you need is to remove a particular element from an array, you're better off by just filtering it:如果您需要从数组中删除特定元素,则最好只对其进行过滤:

 const newArray = ['Apple', 'Orange', 'Plums', 'Grapes']; // Remove the first 'Orange'. const orangeIx = newArray.indexOf('Orange'); console.log('Removed by index:', newArray.filter((ignoredValue, index) => index != orangeIx)); // Remove every 'Orange'. console.log('Removed by value:', newArray.filter(fruit => fruit != 'Orange'));

Note that the above creates a new shallow copy of the original array, not mutates it.请注意,上面创建了原始数组的新浅表副本,而不是对其进行变异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM