I am displaying to the user a series of divs with content in them and the user is able to remove the div from the page so that they no longer see it.
我向用户显示一系列包含内容的div,用户可以从页面中删除div,以便他们不再看到它。
I would like to take the same div content that was removed from an array and add it into another array that will be used to populate a dropdown list.
我想采用从数组中删除的相同div内容,并将其添加到另一个用于填充下拉列表的数组中。
this is what i have:
这就是我所拥有的:
//Remove function
removeCategory(index: number): void {
this.categories.splice(index, 1);
}
//Array
categories: Array<Item> = [
new Item(1, 'test1'),
new Item(2, 'test2')
];
//New Array to add previous removed item
ddoptions: Array<object> = [];
Is it possible to do a push statement before the splice in the removeCategory function? I am not sure of what to pass in since doing this gives me an error:
是否可以在removeCategory函数中的splice之前执行push语句?我不确定要传递什么,因为这样做会给我一个错误:
this.ddoptions.push(index);
3 个解决方案
#1
3
You can splice into the push method directly, as splice returns the removed object.
您可以直接拼接到push方法,因为splice返回已删除的对象。
this.ddoptions.push(this.categories.splice(index, 1)[0]);
The splice method actually returns an array of elements that were removed. In your case, you are only removing one so the above code works. If you are removing one or more elements, you can add these to the second array with several methods.
splice方法实际上返回一个被删除的元素数组。在您的情况下,您只删除一个,所以上面的代码工作。如果要删除一个或多个元素,可以使用多种方法将这些元素添加到第二个数组中。
By using concat to generate a new array:
通过使用concat生成一个新数组:
this.ddoptions = this.ddoptions.concat(this.categories.splice(index, 1));
This may break things because ddoptions is no longer the same object. It depends on what you're doing and what angular is doing.
这可能会破坏事物,因为ddoptions不再是同一个对象。这取决于你正在做什么以及角度正在做什么。
You can pass the array as multiple arguments to push by applying the push prototype.
您可以通过应用推送原型将数组作为多个参数传递。
[].push.apply(this.ddoptions, this.categories.splice(index, 1));
In this way ddoptions will remain as the same array and will push all new elements into it.
这样,ddoptions将保持为相同的数组,并将所有新元素推入其中。
#2
2
Splice returns an array of the deleted elements. You could maybe add a size test of the array, to be sure there is an element inside..
Splice返回已删除元素的数组。你可以添加一个数组的大小测试,以确保里面有一个元素..
//Remove function
removeCategory(index: number): void {
var deletedElement = this.categories.splice(index, 1);
ddoptions.push(deletedElement[0]);
}
#3
2
Array#splice
conveniently returns an array containing the deleted elements:
Array#splice方便地返回包含已删除元素的数组:
removeCategory (index) {
this.ddoptions.push( this.categories.splice(index, 1)[0] )
}
View TypeScript Demo
JavaScript Demo:
class Item {
constructor(a, b) { return [a, b] }
}
class Example {
removeCategory (index) {
this.ddoptions.push( this.categories.splice(index, 1)[0] )
}
//Array
categories = [
new Item(1, 'test1'),
new Item(2, 'test2')
]
//New Array to add previous removed item
ddoptions = []
}
var example = new Example()
example.removeCategory(1)
console.log(example.ddoptions)
#1
3
You can splice into the push method directly, as splice returns the removed object.
您可以直接拼接到push方法,因为splice返回已删除的对象。
this.ddoptions.push(this.categories.splice(index, 1)[0]);
The splice method actually returns an array of elements that were removed. In your case, you are only removing one so the above code works. If you are removing one or more elements, you can add these to the second array with several methods.
splice方法实际上返回一个被删除的元素数组。在您的情况下,您只删除一个,所以上面的代码工作。如果要删除一个或多个元素,可以使用多种方法将这些元素添加到第二个数组中。
By using concat to generate a new array:
通过使用concat生成一个新数组:
this.ddoptions = this.ddoptions.concat(this.categories.splice(index, 1));
This may break things because ddoptions is no longer the same object. It depends on what you're doing and what angular is doing.
这可能会破坏事物,因为ddoptions不再是同一个对象。这取决于你正在做什么以及角度正在做什么。
You can pass the array as multiple arguments to push by applying the push prototype.
您可以通过应用推送原型将数组作为多个参数传递。
[].push.apply(this.ddoptions, this.categories.splice(index, 1));
In this way ddoptions will remain as the same array and will push all new elements into it.
这样,ddoptions将保持为相同的数组,并将所有新元素推入其中。
#2
2
Splice returns an array of the deleted elements. You could maybe add a size test of the array, to be sure there is an element inside..
Splice返回已删除元素的数组。你可以添加一个数组的大小测试,以确保里面有一个元素..
//Remove function
removeCategory(index: number): void {
var deletedElement = this.categories.splice(index, 1);
ddoptions.push(deletedElement[0]);
}
#3
2
Array#splice
conveniently returns an array containing the deleted elements:
Array#splice方便地返回包含已删除元素的数组:
removeCategory (index) {
this.ddoptions.push( this.categories.splice(index, 1)[0] )
}
View TypeScript Demo
JavaScript Demo:
class Item {
constructor(a, b) { return [a, b] }
}
class Example {
removeCategory (index) {
this.ddoptions.push( this.categories.splice(index, 1)[0] )
}
//Array
categories = [
new Item(1, 'test1'),
new Item(2, 'test2')
]
//New Array to add previous removed item
ddoptions = []
}
var example = new Example()
example.removeCategory(1)
console.log(example.ddoptions)