Check if dictionary is null or empty?

Asked

Viewed 901 times

0

In a return of a method I have a resulting dictionary, sometimes it may come empty, hence I’m having trouble identifying when it is empty or not.

for(var i = 1; i <= this.last; i++)
{
    this.select(i, (item) => 
    {
        console.debug(JSON.stringify(item));
        if(item != null)
            all.push(item);
    });
}

The debug output is as follows:

database.ts:104 {"id":"1","descricao":"Alimentação"}
database.ts:104 {"id":"2","descricao":"Transporte"}
database.ts:104 {"id":"3","descricao":"Saúde"}
database.ts:104 {"id":"4","descricao":"Educação"}
database.ts:104 {}
database.ts:104 {}
database.ts:104 {"id":"7","descricao":"Mais uma conta"}

I want to run the all.push(item) only when item is not vazio, tried to use the .length but it doesn’t work, I put console.log(item.length) and in all cases returns undefined. How can I check if it’s empty?

  • Set empty. No further information not sure if you can answer this.

  • Empty: Without any information inside, ie {}, I’ve already solved by manipulating the method select to return false if you are unsuccessful in taking the data from localStorage. But I did not post the answer because it is not equivalent to the question. It would be to do the equivalent of if(!empty($array)) in php and if(array.size() > 0) in Java. But I didn’t find any option to check this in Typescript

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.