Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
¿Cómo filtrar una matriz de objetos y filtrar valores en función de otra matriz? El filtrado debe ocurrir en función de las claves, no de los valores.

Tengo una matriz de objetos que debe filtrarse en función de otra matriz, las claves se enumeran en la matriz permitida, los pls ayudan a cansarse de usar object.entries y reducir pero no funcionó

 const filter = _.filter; const data = [{ id: 1, row: [{ id: 'a', name: 'ab', code: 'sdf', version: 1 }, { id: 'b', name: 'bc', code: 'def', version: 3 }, { id: 'c', name: 'cd', code: 'afd', version: 2 }, ] }, { id: 2, row: [{ id: 'd', name: 'ef', code: 'sdf', version: 1 }, { id: 'e', name: 'gh', code: 'def', version: 3 }, { id: 'f', name: 'ij', code: 'afd', version: 2 }, ] }, { id: 3, row: [{ id: 'g', name: 'kl', code: 'asd', version: 2 }, { id: 'h', name: 'mn', code: 'faf', version: 3 }, { id: 'i', name: 'op', code: 'dfs', version: 1 }, ] } ] const allowed = ['id', 'name'] let result = [{ id: 1, row: [{ id: 'a', name: 'ab' }, { id: 'b', name: 'bc' }, { id: 'c', name: 'cd' }, ] }, { id: 2, row: [{ id: 'd', name: 'ef' }, { id: 'e', name: 'gh' }, { id: 'f', name: 'ij' }, ] }, { id: 3, row: [{ id: 'g', name: 'kl' }, { id: 'h', name: 'mn' }, { id: 'i', name: 'op' }, ] } ] result = data.filter(el => el.row.filter(elm => Object.fromEntries(allowed.map(k => [k, elm[k]])))); console.log(result);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede crear una nueva matriz con Array.map .

Lógica

  • Mapa a través de la matriz.
  • Simplemente extienda el operador para separar la clave de row y el resto de las claves.
  • devuelve un objeto con el resto de claves y la clave de fila como con Object.fromEntries

 const data = [{ id: 1, row: [ { id: 'a', name: 'ab', code: 'sdf', version: 1 }, { id: 'b', name: 'bc', code: 'def', version: 3 }, { id: 'c', name: 'cd', code: 'afd', version: 2 }, ] }, { id: 2, row: [ { id: 'd', name: 'ef', code: 'sdf', version: 1 }, { id: 'e', name: 'gh', code: 'def', version: 3 }, { id: 'f', name: 'ij', code: 'afd', version: 2 }, ] }, { id: 3, row: [ { id: 'g', name: 'kl', code: 'asd', version: 2 }, { name: 'mn', code: 'faf', version: 3 }, { id: 'i', name: 'op', code: 'dfs', version: 1 }, ] } ] const allowed = ['id', 'name']; const result = data.map(({ row, ...rest }) => { return { ...rest, row: row.map(elm => Object.fromEntries(allowed.map(k => [k, elm[k]]))) } }); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Largo camino pero funciona:

 const data = [ {id: 1, row: [ {id: 'a', name: 'ab', code: 'sdf', version: 1}, {id: 'b', name: 'bc', code: 'def', version: 3}, {id: 'c', name: 'cd', code: 'afd', version: 2}, ] }, {id: 2, row: [ {id: 'd', name: 'ef', code: 'sdf', version: 1}, {id: 'e', name: 'gh', code: 'def', version: 3}, {id: 'f', name: 'ij', code: 'afd', version: 2}, ] }, {id: 3, row: [ {id: 'g', name: 'kl', code: 'asd', version: 2}, {id: 'h', name: 'mn', code: 'faf', version: 3}, {id: 'i', name: 'op', code: 'dfs', version: 1}, ] } ]; const allowed = ['id', 'name']; let res = []; data.forEach((el) => { let obj = {}; obj.id = el.id; obj["row"] = []; let row = buildArray(el.row); obj["row"].push(row); res.push(obj); }) function buildArray(row) { r = {}; allowed.forEach((k) => { r[k] = row[0][k]; }) return r; } console.log(res)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!