Thursday, 27 April 2017

How to remove 'undefined' on Object.keys for forEach

Hi Im using node Js and im having a problem on forEach on Object.keys, in the code below when i try to run it, there was an undefined value on the first value of output. For example lets say the value of input variable is:

input = [dog, cat, mouse, horse, pig];

Then when the program runs (refer the code below..) The value of val(with the forEach loop and Object.keys) is

val = undefined dog plus the animal cat plus the animal mouse plus the animal horse plus the animal pig

I hope you guys understand my question, Thanks

        var input = JSON.parse(JSON.stringify(req.body));
        var val;
                Object.keys(input).forEach(function(key) {
                if(key!='undefined')
                {
                  val +=' '+key+' plus the animal';
                }
                });
        console.log(val);


via John Carlo Velasquez

No comments:

Post a Comment