Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Filtre por hijos y devuelva padre más hijos en una matriz anidada de objetos

Tengo una serie de objetos como el que se muestra a continuación y busco obtener el padre (Panel) y el nombre de su hijo cuando busco 'Resumen'

 [ { Dashboard1: [ {name: 'Summary'}, {name: 'SomeThingElse'}, {name: 'SomeThingElse2'} ] }, { Dashboard2: [ {name: 'Summary'}, {name: 'Different'}, {name: 'Different2'} ] }, { Dashboard3: [ {name: 'Summary'}, {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'} ] }, { Dashboard4: [ {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'} {name: 'AnotherDifferent3'}, {name: 'AnotherDifferent4'}, ] } ]

Tengo una entrada que busca 'Resumen' y quiero que regrese solo

 [ { Dashboard1: [ {name: 'Summary'}, ] }, { Dashboard2: [ {name: 'Summary'}, ] }, { Dashboard2: [ {name: 'Summary'}, ] } ]

Tengo esta función que filtra Dashboard4 donde Summary no está presente como elemento secundario pero también devuelve a todos los elementos secundarios.

 function filterByValue(array, value) { return array.filter((data) => JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1); }

¡Gracias!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

 const a = [ { Dashboard1: [ {name: 'Summary'}, {name: 'SomeThingElse'}, {name: 'SomeThingElse2'} ] }, { Dashboard2: [ {name: 'Summary'}, {name: 'Different'}, {name: 'Different2'} ] }, { Dashboard3: [ {name: 'Summary'}, {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'} ] }, { Dashboard4: [ {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'}, {name: 'AnotherDifferent3'}, {name: 'AnotherDifferent4'}, ] } ]; function filterByValue(array, value) { const result = []; array.forEach(e => { const keys = Object.keys(e); const obj = {}; keys.forEach(k => { const matches = e[k].filter(i => i.name === value); if (matches.length > 0) { obj[k] = matches; } }); if (Object.keys(obj).length > 0) { result.push(obj); } }); return result; } console.log(filterByValue(a, 'Summary'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Esta es una función reutilizable para un escenario similar.

 const searchNestedArraySearch = (val, arr, key, childKey) => {

const newArr = arr.map((e, i) => {

 //Get the propert need from nested object via the key and appending the index let firstChildObj = e[`${key}${i + 1}`]; console.log(firstChildObj); // loop through the nested property array const checkProperty = firstChildObj.map((e) => { const valueProp = e[childKey] === val ? true : false; if (valueProp) { return { [childKey]: val }; } else { return null; } }); // remove null value from each array to get specified search const filterEmpty = checkProperty.filter((val) => val !== null); // return array of property containing search value if (filterEmpty.length) { return { [`${key}${i + 1}`]: filterEmpty }; }

});

return newArr.filter((e) => e !== indefinido); };

console.log(searchNestedArray("Resumen", datos, "Panel", "nombre"));

/**

  • Filtrar el valor del objeto anidado
  • @param {Cadena} valor
  • @param {matriz} matriz
  • clave @param {cadena}
  • @param {String} childKey */
about 4 years ago · Juan Pablo Isaza Relatório

0

Dada la matriz, puede usar una función más coincidente como esa:

 const array = [ { Dashboard1: [ {name: 'Summary'}, {name: 'SomeThingElse'}, {name: 'SomeThingElse2'} ] }, { Dashboard2: [ {name: 'Summary'}, {name: 'Different'}, {name: 'Different2'} ] }, { Dashboard3: [ {name: 'Summary'}, {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'} ] }, { Dashboard4: [ {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'}, {name: 'AnotherDifferent3'}, {name: 'AnotherDifferent4'}, ] }] const filterByValue = (array, value) => array.filter(el => { const objValues = Object.values(el).flat() const elementsValues = objValues.map(v => Object.values(v)).flat() return elementsValues.some(v => v.includes(value)) }) console.log(filterByValue(array, 'Summary'));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda