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
-
1The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour.CertainPerformance– CertainPerformance2018年11月24日 10:35:01 +00:00Commented Nov 24, 2018 at 10:35
-
I tried todos.splice(todos.findIndex((todo) => todo.completed), 1); but it wont worked.Darshit– Darshit2018年11月24日 10:37:00 +00:00Commented Nov 24, 2018 at 10:37
-
@Darshit use this Underscore library filter function underscorejs.org/#filterPranesh Janarthanan– Pranesh Janarthanan2018年11月24日 10:47:19 +00:00Commented Nov 24, 2018 at 10:47
-
Please tell me how to delete it with splice method as i cant replace array i want to just remove it from array!Darshit– Darshit2018年11月24日 11:05:12 +00:00Commented Nov 24, 2018 at 11:05
-
That is unfair - you asked a question, which @Fawzi answered correctly, and you accepted. Then you changed the question, and unselected Fawzi's answer. That is disrespectful of people's efforts that they've put into helping you, and it will make people far less inclined to help you now or in the future.CJK– CJK2018年11月24日 11:08:02 +00:00Commented Nov 24, 2018 at 11:08
4 Answers 4
You can try using the javascript array.filter
todos=todos.filter(todo=>!todo.completed);
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
Comments
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.
Output
inCompletes = [{"id":1,"text":"Do laundry","completed":false}];
Comments
Something like this would do the job
var filteredAry = todos.filter(function(e) { return e.competed !== 'true' })
1 Comment
"completed" is misspelled. But, secondly, you've put quotes around true.