This is a smaller version of the array i have but it has the same structure
with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order
const arr = [{
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
},
{
tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
},
{
tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
},
{
tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'
}
];
const finalTags = [];
const finalWeight = [];
// TODO: find a better way to do this
arr.forEach(v => {
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val => {
if (finalTags.indexOf(val) === -1) finalTags.push(val);
});
});
// Ascending order
finalTags.sort();
finalWeight.sort();
what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this