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

144
Views
Get unique values from array values in json array

I'm writing a code where I need to return uniques values from a JSON array. Here my challenge is, I've got these values as an array for one of the keys.

Here is my code.

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));

Here my expected result is to get ["B1", "B2", "B3"]. Please let me know how can I achieve this.

Updated new sample data:

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;
    });

Thanks

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

0

You need to use flat for merge sub array then your code was good:

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));

Reference:

  • Array.prototype.flat()

After new Data posted i add split with trim.

Reference:

  • String.prototype.split()
  • String.prototype.trim()
about 4 years ago · Juan Pablo Isaza Report

0

You can use .flatMap to get all the brand values and pass it to a Set to make it unique.

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!