Javascript 从数组中删除未选中的项
我开发了一个自动完成下拉列表 选择用户时,我将ID存储在用户数组中 当我选择一个用户时,它会被添加到数组中,但当我取消选择该用户时,有没有办法将其从数组中删除?我该怎么做 是否还有一种方法可以将数组转换为字符串,以便输出字符串,例如:“1,2” 谢谢 .tsJavascript 从数组中删除未选中的项,javascript,angular,typescript,Javascript,Angular,Typescript,我开发了一个自动完成下拉列表 选择用户时,我将ID存储在用户数组中 当我选择一个用户时,它会被添加到数组中,但当我取消选择该用户时,有没有办法将其从数组中删除?我该怎么做 是否还有一种方法可以将数组转换为字符串,以便输出字符串,例如:“1,2” 谢谢 .ts users:any [] =[]; itemSelectionChanged(e){ console.log("item",e) if(e.itemData.selected == true){ this.user
users:any [] =[];
itemSelectionChanged(e){
console.log("item",e)
if(e.itemData.selected == true){
this.users.push(e.itemData.ID);
console.log(this.users)
//output as a string and not an array.... like "1,2"
}
else{
//Remove the unselected value in the array this.users e.itemData.ID
}
}
.html
<dx-drop-down-box [(value)]="treeBoxValue" valueExpr="ID" displayExpr="name" placeholder="Select a value..."
[showClearButton]="true" [dataSource]="treeDataSource" (onValueChanged)="syncTreeViewSelection()">
<div *dxTemplate="let data of 'content'">
<dx-tree-view [dataSource]="treeDataSource" dataStructure="plain" selectionMode="multiple"
showCheckBoxesMode="normal" [selectNodesRecursive]="false" displayExpr="name" [searchEnabled]="true"
[selectByClick]="true" (onItemSelectionChanged)="itemSelectionChanged($event)">
</dx-tree-view>
</div>
</dx-drop-down-box>
尝试以下代码,在else语句中复制
this.users=this.users.filter(x=>x!=e.itemData.ID)
尝试以下代码,在else语句中复制this.users=this.users.filter(x=>x!=e.itemData.ID)
您可以使用Array.prototype.filter
从数组中删除该项。您可以使用Array.prototype.join
将数组元素与分隔符(如逗号)组合:
itemSelectionChanged(e) {
// Use object destructure to get property `ID`
const { ID } = e.itemData;
if (e.itemData.selected) {
// Check if element is in array so a duplicate is not added
// Note: this only works with primitive values, not objects
if (this.users.indexOf(ID) < 0) {
this.users.push(ID);
}
// Join array items with comma character
console.log(this.users.join(","));
} else {
// Remove items using filter
this.users = this.users.filter(user => user !== ID);
}
}
itemSelectionChanged(e){
//使用对象分解结构获取属性'ID'`
const{ID}=e.itemData;
如果(如itemData.selected){
//检查元素是否在数组中,以便不添加重复项
//注意:这仅适用于基本体值,而不适用于对象
if(this.users.indexOf(ID)<0){
这个.users.push(ID);
}
//使用逗号字符联接数组项
console.log(this.users.join(“,”);
}否则{
//使用筛选器删除项目
this.users=this.users.filter(用户=>user!==ID);
}
}
这是一个正在运行的项目。您可以使用
Array.prototype.filter
从数组中删除该项目。您可以使用Array.prototype.join
将数组元素与分隔符(如逗号)组合:
itemSelectionChanged(e) {
// Use object destructure to get property `ID`
const { ID } = e.itemData;
if (e.itemData.selected) {
// Check if element is in array so a duplicate is not added
// Note: this only works with primitive values, not objects
if (this.users.indexOf(ID) < 0) {
this.users.push(ID);
}
// Join array items with comma character
console.log(this.users.join(","));
} else {
// Remove items using filter
this.users = this.users.filter(user => user !== ID);
}
}
itemSelectionChanged(e){
//使用对象分解结构获取属性'ID'`
const{ID}=e.itemData;
如果(如itemData.selected){
//检查元素是否在数组中,以便不添加重复项
//注意:这仅适用于基本体值,而不适用于对象
if(this.users.indexOf(ID)<0){
这个.users.push(ID);
}
//使用逗号字符联接数组项
console.log(this.users.join(“,”);
}否则{
//使用筛选器删除项目
this.users=this.users.filter(用户=>user!==ID);
}
}
这是一个正在运行的元素。您可以使用
splice
array函数从数组中删除元素。请参阅下面的代码
itemSelectionChanged(e) {
console.log("item",e)
if(e.itemData.selected == true){
this.users.push(e.itemData.ID);
console.log(this.users.join());
//output as a string and not an array.... like "1,2"
} else {
const indexOfRemovedElementID = this.users.indexOf(e.itemData.ID);
this.users.splice(indexOfRemovedElementID, 1);
console.log(this.users.join());
//Remove the unselected value in the array this.users e.itemData.ID
}
}
您可以使用
splice
array函数从阵列中删除元素。请参阅下面的代码
itemSelectionChanged(e) {
console.log("item",e)
if(e.itemData.selected == true){
this.users.push(e.itemData.ID);
console.log(this.users.join());
//output as a string and not an array.... like "1,2"
} else {
const indexOfRemovedElementID = this.users.indexOf(e.itemData.ID);
this.users.splice(indexOfRemovedElementID, 1);
console.log(this.users.join());
//Remove the unselected value in the array this.users e.itemData.ID
}
}
除了
filter()
,您还可以使用withindexOf()
从数组中删除项。在else
块中包括以下内容
const index=this.users.indexOf(e.itemData.ID,0);
如果(索引>-1){
本.用户.拼接(索引,1);
}
我已经延长了你的期限
filter()
和splice()
之间的区别是,当filter()
返回一个新数组时,splice()
在适当的位置修改数组。因此,如果使用filter()
,则需要将其重新分配给变量,如this.users=this.users.filter(…)
,而splice()
不需要重新分配。除了filter()
之外,还可以使用withindexOf()
从数组中删除项。在else
块中包括以下内容
const index=this.users.indexOf(e.itemData.ID,0);
如果(索引>-1){
本.用户.拼接(索引,1);
}
我已经延长了你的期限
filter()
和splice()
之间的区别是,当filter()
返回一个新数组时,splice()
在适当的位置修改数组。因此,如果使用filter()
,则需要将其重新分配给变量,如this.users=this.users.filter(…)
,而splice()
不需要重新分配。这里是一个带显示和过滤的变量