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

130
Views
How to combine all unique element in any given array property from array of object into single array in javascript?

I have below javascript array of object problem in which i have tag array property inside array of object. Tag array contain some value in each array of object. I wanted to get unique tag value from each array of object and combine into single array which will contain only unique tag.

const obj = [{
  name: "ABC",
  tag: ["abc", "xyz"]
}, {
  name: "USA",
  tag: ["abc", "suv"]
}, {
  name: "ABC",
  tag: ["pot", "xyz"]
}]

I need the unique tag item into single array as per below.

const unique = ["abc", "xyz", "suv", "pot"];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could use flatMap and Set

const obj = [
  { name: 'ABC', tag: ['abc', 'xyz'] },
  { name: 'USA', tag: ['abc', 'suv'] },
  { name: 'ABC', tag: ['pot', 'xyz'] },
]

const res = [...new Set(obj.flatMap(({tag}) => tag))];

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

A set will give you unique values:

  • Set
  • Array.prototype.flatMap()
  • Spread syntax (...)
  • Unpacking properties from objects passed as a function parameter

const obj = [{
  name: "ABC",
  tag: ["abc", "xyz"]
}, {
  name: "USA",
  tag: ["abc", "suv"]
}, {
  name: "ABC",
  tag: ["pot", "xyz"]
}]

const unique = [...new Set(obj.flatMap(({tag}) => tag))];

console.log(unique)

about 4 years ago · Juan Pablo Isaza Report

0

You can use Set to filtered out the unique elements from an array.

Demo :

const obj = [{
  name: "ABC",
  tag: ["abc", "xyz"]
}, {
  name: "USA",
  tag: ["abc", "suv"]
}, {
  name: "ABC",
  tag: ["pot", "xyz"]
}];

let arr = [];

obj.forEach((item) => {
    arr.push(...item.tag)
})

console.log([...new Set(arr)]);

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!