Tim*_*ond 321 arrays collections typescript
我有一个我在TypeScript中创建的数组,它有一个我用作键的属性.如果我有该密钥,我该如何从中删除一个项目?
blo*_*ish 532
与在JavaScript中一样.
delete myArray[key];
Run Code Online (Sandbox Code Playgroud)
请注意,这会将元素设置为undefined
.
最好使用这个Array.prototype.splice
功能:
const index = myArray.indexOf(key, 0);
if (index > -1) {
myArray.splice(index, 1);
}
Run Code Online (Sandbox Code Playgroud)
Mal*_*zad 158
如果array是对象的类型,那么最简单的方法是
let foo_object // Item to remove
this.foo_objects = this.foo_objects.filter(obj => obj !== foo_object);
Run Code Online (Sandbox Code Playgroud)
小智 50
使用ES6,您可以使用以下代码:
removeDocument(doc){
this.documents.forEach( (item, index) => {
if(item === doc) this.documents.splice(index,1);
});
}
Run Code Online (Sandbox Code Playgroud)
小智 18
这是一个简单的单行代码,用于从对象数组中按属性删除对象。
delete this.items[this.items.findIndex(item => item.item_id == item_id)];
Run Code Online (Sandbox Code Playgroud)
或者
this.items = this.items.filter(item => item.item_id !== item.item_id);
Run Code Online (Sandbox Code Playgroud)
Buz*_*zzy 16
这是我的解决方案:
onDelete(id: number) {
this.service.delete(id).then(() => {
let index = this.documents.findIndex(d => d.id === id); //find index in your array
this.documents.splice(index, 1);//remove element from array
});
event.stopPropagation();
}
Run Code Online (Sandbox Code Playgroud)
小智 14
您可以splice
在数组上使用该方法来删除元素.
例如,如果您有一个名称arr
使用以下数组:
arr.splice(2,1);
Run Code Online (Sandbox Code Playgroud)
所以这里索引为2的元素将是起点,参数2将决定要删除的元素数量.
如果要删除命名数组的最后一个元素,arr
请执行以下操作:
arr.splice(arr.length,1);
Run Code Online (Sandbox Code Playgroud)
这将返回arr并删除最后一个元素.
Ven*_*ndi 13
Typescript/Javascript 中的多个选项可用于从数组中删除元素。拼接是最好的选择
下面是使用 Splice 函数根据对象数组中的某些字段删除对象的示例
const persons = [
{
firstName :'John',
lastName :'Michel'
},
{
firstName :'William',
lastName :'Scott'
},
{
firstName :'Amanda',
lastName :'Tailor'
}
]
console.log('Before Deleting :'+JSON.stringify(persons));
console.log('Deleting William:');
persons.splice(persons.findIndex(item => item.firstName === 'William'),1);
console.log('After Deleting William'+JSON.stringify(persons));
Run Code Online (Sandbox Code Playgroud)
小智 12
这对我有用。
你的数组:
DummyArray: any = [
{ "id": 1, "name": 'A' },
{ "id": 2, "name": 'B' },
{ "id": 3, "name": 'C' },
{ "id": 4, "name": 'D' }
]
Run Code Online (Sandbox Code Playgroud)
功能:
remove() {
this.DummyArray = this.DummyArray.filter(item => item !== item);
}
Run Code Online (Sandbox Code Playgroud)
注意:此函数删除数组中的所有对象。如果要从数组中删除特定对象,请使用此方法:
remove(id) {
this.DummyArray = this.DummyArray.filter(item => item.id !== id);
}
Run Code Online (Sandbox Code Playgroud)
Lin*_*adu 10
如果您需要从数组中删除给定的对象并且希望确保以下内容,请使用此选项:
const objWithIdToRemove;
const objIndex = this.objectsArray.findIndex(obj => obj.id === objWithIdToRemove);
if (objIndex > -1) {
this.objectsArray.splice(objIndex, 1);
}
Run Code Online (Sandbox Code Playgroud)
让部门是一个数组。您要从此数组中删除一个项目。
departments: string[] = [];
removeDepartment(name: string): void {
this.departments = this.departments.filter(item => item != name);
}
Run Code Online (Sandbox Code Playgroud)
使用TypeScript传播运算符回答(...)
// Your key
const key = 'two';
// Your array
const arr = [
'one',
'two',
'three'
];
// Get either the index or -1
const index = arr.indexOf(key); // returns 0
// Despite a real index, or -1, use spread operator and Array.prototype.slice()
const newArray = (index > -1) ? [
...arr.slice(0, index),
...arr.slice(index + 1)
] : arr;
Run Code Online (Sandbox Code Playgroud)
使用Typescript的另一种解决方案:
let updatedArray = [];
for (let el of this.oldArray) {
if (el !== elementToRemove) {
updated.push(el);
}
}
this.oldArray = updated;
Run Code Online (Sandbox Code Playgroud)
let a: number[] = [];
a.push(1);
a.push(2);
a.push(3);
let index: number = a.findIndex(a => a === 1);
if (index != -1) {
a.splice(index, 1);
}
console.log(a);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
406683 次 |
最近记录: |