letseasy
letseasy

Reputation: 47

Javascript : return object?

I am trying to return object from function but it returns nothing any idea?

var todo = [{
    id: 'a01',
    titre: 'Citation',
    message: 'Vous êtes de ceux-là ? Ca tombe bien, je lai été moi aussi !',
    completed: false
}];

getById: function(todoId) {
    for (var i = 0; i < todo.length; i++) {
        if (todo[i].id == todoId) {
            return todo;
        }
    }
}

Upvotes: 1

Views: 97

Answers (2)

Mr. X
Mr. X

Reputation: 877

Try this code:

var todo=[{
    id:'a01',
    titre:'Citation',
    message:'Vous êtes de ceux-là ? Ca tombe bien, je lai été moi aussi !',
    completed:false
}];

var getById = function(todoId){
    for(var i=0;i<todo.length;i++){
        //console.log(todo[i].id == todoId);
        if(todo[i].id == todoId){
            return todo[i];
        }
    }
}

Upvotes: -1

R. Oosterholt
R. Oosterholt

Reputation: 8080

return the array element instead of the complete array:

getById: function(todoId){
    for(var i=0;i<todo.length;i++){
        //console.log(todo[i].id == todoId);
        if(todo[i].id == todoId){
            return todo[i];
        }
    }
}

Upvotes: 2

Related Questions