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

188
Vistas
problems filling an array with javascript

I parsed a json and I'm trying to take 2 values for each element from the json and put them in a array the problem is that I want to put the values into the array like a single element "array" example:

[
 { name: 'name1', elements: [ 'elem1' ] },
 { name: 'name2', elements: [ 'elem2', 'elem3' ] }
]

I tried 2 ways.

the first is this:

function getMonsters(json) {
    var monsters = [];
    var monster = {};
    json.forEach(element => {
        if (element.type === "large") {
            monster['name'] = element.name;
            monster['elements'] = element.elements;
            monsters.push(monster);
        }
    });
    return monsters;
}

the problem with the first way is that it always returns the same 2 values: response image

the second way is this:

function getMonsters(json) {
    var monsters = [];
    var monster = {};
    json.forEach(element => {
        if (element.type === "large") {
            monsters.push(element.name, element.elements);
        }
    });
    return monsters;
}

but the problem with the second way is that it returns each monster and element separately and not like in my example: response image

this is the json if u want to check : https://mhw-db.com/monsters

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are reusing the monster object every iteration in your first example. Either move the declaration of var monster = {} into the loop or, better yet, just push an object literal.

function getMonsters(json) {
  const monsters = [];

  json.forEach(({ elements, name, type }) => {
    if (type === "large") {
       monsters.push({ name, elements });
    }
  });

  return monsters;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your first attempt is almost correct. The reason why all of the items in the array end up being the same object is because monster is the same reference in all of the array items. You need a new instance of monster on every iteration. Just put your initialization of monster in your loop

function getMonsters(json) {
var monsters = [];
json.forEach(element => {
    if (element.type === "large") {
        var monster = {};
        monster['name'] = element.name;
        monster['elements'] = element.elements;
        monsters.push(monster);
    }
});
return monsters;
about 4 years ago · Juan Pablo Isaza 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