YoniGeek
YoniGeek

Reputation: 4093

how can I do this without using underscoreJS?

I'm wondering how I could do this using JUST javascript without using undercoreJS? I've been trying different ways but I can't make it happen! :( Thanks!

What I want to do is to check which is checked or unchecked basically.

myArray =[
        {text: 'lean something', done: false},
        {text: 'what ever', done: false }

    ];

function clearCompleted = function(){
        myArray = _.filter(myArray, function(todo){
                    return !todo.done;
                });

Upvotes: 0

Views: 49

Answers (3)

Blender
Blender

Reputation: 298166

If you support only modern browsers (i.e. anything newer than IE8), you can just use .filter():

myArray.filter(function(todo){
    return !todo.done;
});

If you want to support older browsers, use a loop:

var temp = [];

for (var i = 0; i < myArray.length; i++) {
    if (!myArray[i].done) {
        temp.push(myArray[i]);
    }
}

Upvotes: 3

Alnitak
Alnitak

Reputation: 339816

You can use Array.filter:

myArray = myArray.filter(function(todo) {
    return !todo.done;
});

If you're on an older browser that doesn't include that method there's a shim available at the above link.

Upvotes: 2

Bergi
Bergi

Reputation: 664444

You can use the native filter method:

myArray = myArray.filter(function(todo){
    return !todo.done;
});

Upvotes: 0

Related Questions