0

How to iterate in this object which is post result from form and get only the elements which have value. I want to make check if key has value write in database I tried using for in, map etc. but unsucesfully { Flouer:'12', Milk:'3', Oil:'11', Salt:'', Eggs:'', Tomatos:'', Meat:'' }

  • Possible duplicate of [JavaScript: filter() for Objects](https://stackoverflow.com/questions/5072136/javascript-filter-for-objects) – Nick Nov 21 '19 at 21:32

1 Answers1

0

Simply iterate on the object and check for the emptiness of the value

e.g: on nodejs console:

> const st = {
...     Flouer:'12',
...     Milk:'3',
...     Oil:'11',
...     Salt:'',
...     Eggs:'',
...     Tomatos:'',
...     Meat:''
...   }
undefined
> st
{ Flouer: '12',
  Milk: '3',
  Oil: '11',
  Salt: '',
  Eggs: '',
  Tomatos: '',
  Meat: '' }

>for (var po in st) { if (Object.keys(a1[po]).length > 0 )  { console.log("value of the key " + po + " value is " + a1[po]) } }

Outputs:

value of the key Flouer value is 12
value of the key Milk value is 3
value of the key Oil value is 11
krishna Prasad
  • 3,541
  • 1
  • 34
  • 44