I have this array, where ‘data_type’ holds values in a comma separated string:
Original array:
JavaScript
x
7
1
var getGroups = [
2
{ id: 52, data_type: "Prices & Volumes,Holdings,Reference Data" },
3
{ id: 51, data_type: "Prices & Volumes,Holdings,Reference Data" },
4
{ id: 49, data_type: "Fundamentals,Holdings,Corporate Actions,Reference Data" },
5
{ id: 25, data_type: "Holdings,Corporate Actions,Reference Data" }
6
]
7
How to catch, filter and build new array with all unique values?
Target array:
JavaScript
1
13
13
1
var targetArray = [
2
"Prices & Volumes",
3
"Holdings",
4
"Reference Data",
5
"Fundamentals",
6
"Corporate Actions"
7
]
8
9
console.log('targetArray:');
10
console.log(targetArray);
11
12
["Prices & Volumes", "Holdings", "Reference Data", "Fundamentals", "Corporate Actions"]
13
Here is a Fiddle: https://jsfiddle.net/grnewkzs/1/
Advertisement
Answer
JavaScript
1
11
11
1
const getGroups = [
2
{ id: 52, data_type: "Prices & Volumes,Holdings,Reference Data" },
3
{ id: 51, data_type: "Prices & Volumes,Holdings,Reference Data" },
4
{ id: 49, data_type: "Fundamentals,Holdings,Corporate Actions,Reference Data" },
5
{ id: 25, data_type: "Holdings,Corporate Actions,Reference Data" }
6
];
7
8
const values = getGroups.map(item => item.data_type.split(','))
9
const sets = new Set([].concat(values))
10
const array = Array.from(sets)
11
you need to know why and how, or your lean nothing…that’s bad.