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

Darshit
  • 85
  • 3
  • 12
  • 1
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), 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 [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Nov 24 '18 at 10:35
  • I tried todos.splice(todos.findIndex((todo) => todo.completed), 1); but it wont worked. – Darshit Nov 24 '18 at 10:37
  • @Darshit use this Underscore library filter function https://underscorejs.org/#filter – Pranesh Janarthanan Nov 24 '18 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 Nov 24 '18 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 Nov 24 '18 at 11:08
  • I appreciate @fawzi for its efforts Thanks – Darshit Nov 24 '18 at 12:01

4 Answers4

3

You can try using the javascript array.filter

todos=todos.filter(todo=>!todo.completed);
Fawzi
  • 359
  • 2
  • 12
  • never used angular before, but check [this](https://stackoverflow.com/questions/34827334/triggering-change-detection-manually-in-angular) out as you might be calling the updated outside Angular zone. – Fawzi Nov 24 '18 at 11:06
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

Burak Güneli
  • 114
  • 1
  • 11
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}];
Anand S
  • 790
  • 1
  • 7
  • 22
-1

Something like this would do the job

var filteredAry = todos.filter(function(e) { return e.competed !== 'true' })
  • 2
    This won't work because, firstly, `"completed"` is misspelled. But, secondly, you've put quotes around `true`. – CJK Nov 24 '18 at 10:42