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

183
Vistas
Anidamiento para bucle automático

Tengo una matriz con objetos y esos objetos consisten en algunas matrices. Quiero recorrer toda la matriz y la matriz dentro de los objetos. Suena complicado, pero si observa el ejemplo a continuación, funciona. Sin embargo, mi problema es que en este momento la longitud de la matriz de variables es 2, pero ¿cómo puedo implementar algo que haga posible este tipo de bucle en bucle si la matriz tiene una longitud de 4 sin codificarla, ya que obtendré datos de una API que varía? mucho en las variables.

 let wantedArray =[] let array = [ { gender: male, value: 10, age: 5,countryofbirth:"Norway" }, { gender: female, value: 10, age: 2,countryofbirth:"Sweden" }, { gender: male, value: 15, age: 3,countryofbirth:"Norway" }, { gender: male, value: 11, age: 4,countryofbirth:"Norway" }, { gender: female, value: 10, age: 2,countryofbirth:"Finland" }, ... ] let variables = [ { id: gender, options: [male, female] }, { id: "countryofbirth", options: ["Norway", "Sweden", "Denmark", "Finland"]} ] variables[0].options.map((item) => { variables[1].options.map((item2) => { let currArray = array.filter((currData) => currData[variables[0].id] === item && currData[variables[1].id] === item2); //lets say that it have come to the point in the loop where item===male and item2==="Norway" let currObject ={variables[0].id:item//"Male", variables[1].id:item2}//"Norway" let currValues ={} currArray.map((data)=>{ currValues[data.age]=value }) currObject["values"]=currValues wantedArray.push(currObject) /*This means when item===male and item2==="Norway" the function would push { gender:"Male", countryofbirth:"Norway, values:{5:10,3:15,4:11} } to wantedArray*/ }) })
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Supongo que tal vez estés buscando algo como

 const data = [ {gender: "male", value: 10, age: 5, countryofbirth: 'Norway'}, {gender: "female", value: 10, age: 2, countryofbirth: 'Sweden'}, {gender: "male", value: 15, age: 3, countryofbirth: 'Norway'}, {gender: "male", value: 11, age: 4, countryofbirth: 'Norway'}, {gender: "female", value: 10, age: 2, countryofbirth: 'Finland'}, ] // These are all dynamic. const filter = {gender: "male", countryofbirth: 'Norway'}; const valueKey = 'age'; const valueValue = 'value'; // Find objects with key/values matching all of those in `filter`. const matching = data.filter((item) => Object.entries(filter).every(([key, value]) => item[key] === value)); console.log(matching); // Generate a mapping from the found objects using the `valueKey` and `valueValue` variables. const values = Object.fromEntries(matching.map((item) => [item[valueKey], item[valueValue]])); // Merge the filter and the values to get the desired result. console.log({...filter, values});

que finalmente se imprime

 { gender: 'male', countryofbirth: 'Norway', values: { '3': 15, '4': 11, '5': 10 } }
about 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