angular - 按 id 删除数组对象
问题描述
我正在尝试通过 id 删除离子存储中的数组对象。但它没有按预期工作..我该怎么办。这是我的代码
页面.ts
removeItem(id){
this.storage.get('storedAdd').then((val) => {
console.log(Object.keys(val))
Object.keys(val).splice(id, 1)
console.log(val)
for(let element of Object.keys(val)){
console.log(val[element])
val[element].id.toString().splice(id,1)
this.storage.set('storeAdd', val)
console.log(val)
}
}
我使用拼接但收到此错误
ERROR Error: Uncaught (in promise): TypeError: val[element].id.toString(...).splice is not a function
TypeError: val[element].id.toString(...).splice is not a function
解决方案
我认为您不能在那里使用 toString ,因为splice() 方法通过删除或替换现有元素和/或在适当位置添加新元素来更改数组的内容。 删除键的示例为 0:我使用了 delete
let myObject = {
0: {
"id" : 1,
"text": "text1"
},
1: {
"id" : 2,
"text": "text2"
}
};
delete myObject[0];
console.log(myObject);
推荐阅读
- arduino - Arduino Every TCCR1A '未在此范围内声明'
- python-3.x - 如何防止python中的数字太低/太高?
- c++ - 我如何将内存读入 wstring?
- html - CSS 定位:应该在首屏下方的内容出现在首屏之上
- javascript - Javascript在同一位置插入前一个,下一个文件名中的一小组字符
- typescript - Vuex 模块中的 TypeScript 错误 - 子模块无法分配到 Vuex 的索引模块中的错误
- reactjs - ES6 类 getter 函数在刷新后在反应中返回未定义
- bash - 计算反向模量
- sql - SQL 中的 Group By Vs Distinct
- arrays - 如何通过字符串数组过滤对象字段