Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

525
Vistas
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);
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

change your var val; to var val = ''. When you write var val;, val is being assigned undefined onto which you are then concatenating your text. If you instead define val as a blank string, you won't get this problem. Also, as pointed out above, you don't need Object.keys since you are already working with an array

var input = JSON.parse(JSON.stringify(req.body));
var val = '';
input.forEach(function(key) {
  val +=' '+key+' plus the animal';
});
console.log(val);
over 4 years ago · Santiago Trujillo Denunciar

0

Your solution seems fine. You just need to handle the first iteration and no need for Object.keys since input is an array.

var val;
input.forEach(function(key) {
    if(!val && key!='undefined')
        val = key + ' plus';
    else if(key!='undefined')
    {
      val +=' '+key+' plus the animal';
    }
});
console.log(val);
over 4 years ago · Santiago Trujillo Denunciar

0

In JavaScript,if you don't assign a value to your variable when you declare it, it get undefined as defualt. So if you have something like this :

var drink; //The value is undefined now
drink +=' Milk';// now , this will be "undefined Milk"

in your code you have

var val;

value of val is undefined in this line

so you just need to set a value to val when you declare it, in your case it should be a string so :

var val="";

now you not gonna get "undefined" in your output anymore.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda