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

150
Views
Obtenga valores únicos de los valores de la matriz en la matriz json

Estoy escribiendo un código donde necesito devolver valores únicos de una matriz JSON. Aquí mi desafío es que tengo estos valores como una matriz para una de las claves.

Aquí está mi código.

 let mobilePhones = [{ id: 1, brand: ["B1", "B2"] }, { id: 2, brand: ["B2"] }, { id: 3, brand: ["B1", "B2"] }, { id: 4, brand: ["B1"] }, { id: 5, brand: ["B2", "B1"] }, { id: 6, brand: ["B3"] }] let allBrandsArr = mobilePhones.map(row => { return row.brand; }); let uniqueBrands = allBrandsArr.filter((item, index, arry) => (arry.indexOf(item) === index)); console.log(JSON.stringify(uniqueBrands));

Aquí mi resultado esperado es obtener ["B1", "B2", "B3"] . Por favor, hágame saber cómo puedo lograr esto.

Nuevos datos de muestra actualizados:

 let mobilePhones = [{ id: 1, brand: ["B1, B2"] }, { id: 2, brand: ["B2"] }, { id: 3, brand: ["B1, B2"] }, { id: 4, brand: ["B1"] }, { id: 5, brand: ["B2, B1"] }, { id: 6, brand: ["B3"] }] let allBrandsArr = mobilePhones.map(row => { return row.brand; });

Gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe usar flat para fusionar submatriz, entonces su código fue bueno:

 let mobilePhones = [{ id: 1, brand: ["B1, B2"] }, { id: 2, brand: ["B2"] }, { id: 3, brand: ["B1, B2"] }, { id: 4, brand: ["B1"] }, { id: 5, brand: ["B2, B1"] }, { id: 6, brand: ["B3"] }] let allBrandsArr = mobilePhones.map(row => { return row.brand[0].split(',').map(function(item) { return item.trim(); }); }); let uniqueBrands = allBrandsArr.flat().filter((item, index, arry) => (arry.indexOf(item) === index)); console.log(JSON.stringify(uniqueBrands));

Referencia:

  • matriz.prototipo.plano()

Después de publicar nuevos datos, agrego split con trim .

Referencia:

  • Cadena.prototipo.split()
  • Cadena.prototipo.trim()
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar .flatMap para obtener todos los valores de la marca y pasarlo a un Set para que sea único.

 const uniqueBrands = [...new Set(mobilePhones.flatMap(({ brand }) => brand))]; 

 let mobilePhones = [{ id: 1, brand: ["B1", "B2"] }, { id: 2, brand: ["B2"] }, { id: 3, brand: ["B1", "B2"] }, { id: 4, brand: ["B1"] }, { id: 5, brand: ["B2", "B1"] }, { id: 6, brand: ["B3"] }] const unique = [...new Set(mobilePhones.flatMap(({ brand }) => brand))]; console.log(unique);

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!