0

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

6
  • 1
    The 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. Commented Nov 24, 2018 at 10:35
  • I tried todos.splice(todos.findIndex((todo) => todo.completed), 1); but it wont worked. Commented Nov 24, 2018 at 10:37
  • @Darshit use this Underscore library filter function underscorejs.org/#filter Commented 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! Commented 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. Commented Nov 24, 2018 at 11:08

4 Answers 4

3

You can try using the javascript array.filter

todos=todos.filter(todo=>!todo.completed);
Sign up to request clarification or add additional context in comments.

1 Comment

never used angular before, but check this out as you might be calling the updated outside Angular zone.
0

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

0
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

-1

Something like this would do the job

var filteredAry = todos.filter(function(e) { return e.competed !== 'true' })

1 Comment

This won't work because, firstly, "completed" is misspelled. But, secondly, you've put quotes around true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.