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

143
Views
¿Cómo filtrar una matriz de objetos dentro de la matriz del tipo de objeto en JavaScript?

Estoy enfrentando un problema con una de las respuestas de la API, tengo una respuesta similar a la siguiente.

 [ {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "US"}, {type: "County", countyName: "US"}, {type: "County", countyName: "US"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "German"}, {type: "County", countyName: "German"}, {type: "County", countyName: "German"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "Japan"}, {type: "County", countyName: "German"}, {type: "County", countyName: "German"}]}, ]

parece una matriz de objetos dentro que tienen países y también las matrices del tipo de objeto. Necesito todo el nombre del condado en una sola matriz para toda la matriz de elementos. como el siguiente tipo,

 newNames = [US, German, Japan];

Intenté algo como esto. pero no pude obtener la salida. ¿Puedes ayudarme a hacer esto? gracias a todos.

 let newNames = this.selectedStateList.filter(item => item.countries.forEach(item => item.countries)).map(ele => ele.countries.forEach(item => item.countries))
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede mirar a través de cada objeto de countries y agregar sus datos en un Set para obtener un país único.

 const data = [ {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "US"}, {type: "County", countyName: "US"}, {type: "County", countyName: "US"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "German"}, {type: "County", countyName: "German"}, {type: "County", countyName: "German"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "Japan"}, {type: "County", countyName: "German"}, {type: "County", countyName: "German"}]}, ], result = Array.from(data.reduce((r, {countries}) => { countries.forEach(({countyName}) => r.add(countyName)); return r; }, new Set())); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Necesita .map s, no .forEach - .map transforms each element of an array, but forEach` no devuelve nada.

Después de obtener una matriz de matrices de nombres de países, puede aplanar con .flat() , o puede juntarlos y usar flatMap en su lugar.

 const input = [ {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "US"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "German"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "Japan"}]}, ]; const output = input.flatMap( obj => obj.countries.map( country => country.countyName ) ); console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

Algo como esto funcionaría?

 const arr = [ {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "US"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "German"}]}, {type: "StateCountry", state: "AL", countries: [{type: "County", countyName: "Japan"}]} ] const result = arr.reduce((acc,val)=>{ acc.push(val.countries[0].countyName) return acc },[] ) console.log(result)

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!