[英]Delete particular objects from array in javascript
I have a object of array stored in local-storage as shown below 我有一个存储在本地存储中的数组对象,如下所示
var todos = [
{"id":0,"text":"Make lunch","completed":true},
{"id":1,"text":"Do laundry","completed":false},
{"id":2,"text":"Complete Project","completed":true}
]
How can i delete all objects that are completed? 如何删除所有已完成的对象? Please tell me how to delete it with splice method as i cant replace array i want to just remove it from array!(as my project requirements) Thanks for any help 请告诉我如何使用拼接方法删除它,因为我无法替换数组,我只想将其从数组中删除!(作为我的项目要求)感谢您的帮助
您可以尝试使用javascript array.filter
todos=todos.filter(todo=>!todo.completed);
这样的事情会做的
var filteredAry = todos.filter(function(e) { return e.competed !== 'true' })
You can filter uncompleted todos like: 您可以过滤未完成的待办事项,例如:
const unCompletedTodos = todos.filter(todo => !todo.completed);
Also you don't have to use const
you can use var
but I recommend you to use const
or let
instead of var
另外,您不必使用const
即可使用var
但我建议您使用const
或let
代替var
var todos = [
{"id":0,"text":"Make lunch","completed":true},
{"id":1,"text":"Do laundry","completed":false},
{"id":2,"text":"Complete Project","completed":true} ];
var inCompletes = todos.filter(item => !item.completed );
inCompletes will return an array of objects with completed is false. inCompletes将返回一个对象数组,其中completed为false。
Output 产量
inCompletes = [{"id":1,"text":"Do laundry","completed":false}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.