Modify and manipulate JavaScript array
You can use reduce()
and Set
combination for example. Read from the docs:
The
Set
object lets you store unique values of any type, whether primitive values or object references.The
reduce()
method executes a reducer function (that you provide) on each element of the array, resulting in single output value.
Please see a possible working solution.
const array = ['business>management', 'News>Entertainment News', 'business>Entrepreneurship'];
const result = array.reduce((a,c) => {
c.split('>').forEach(e => a.add(e));
return a;
}, new Set());
const unique = Array.from(result);
console.log(unique);
I hope that helps!
try this one -
const array = ['business>management', 'News>Entertainment News', 'business>Entrepreneurship'];
let newArray = []
array.map((item)=>{
let newData = item.split(">").map((itemIn)=>{
newArray.push(itemIn)
return item
})
return newData
})
console.log(newArray)
Simplified
let array = ['business>management', 'News>Entertainment News', 'business>Entrepreneurship'];
let separated = array.map((item, ii) => {
return item.split(">")
}).reduce((a, b) => a.concat(b)).filter((value, index, self) => {
return self.indexOf(value) === index;
});
console.log(separated)