I understand that some may consider this a duplicate, but I have yet to come across a similar example that I can relate to with my limited knowledge. So, I apologize in advance :)
This should be pretty simple for an experienced JS developer, I assume.
My struggle lies in trying to match on an array's property:
if (!uniqueEnts.includes(ent.entity)) { // how do I match on uniqueEnts.entityName ?
The current approach is yielding too many results in the new list, and the check above doesn't provide the correct comparison against uniqueEnts.entityName.
JSON Input : https://i.sstatic.net/qSHAt.png
Desired output: A new list (uniqueEntities) with two properties:
- entityName (unique)
- entityColor (randomly generated)
Code:
uniqueEntities() {
let uniqueEnts = []
this.nluDataUnfiltered.forEach(function (i) {
i.entities.forEach(function (ent) {
if (!uniqueEnts.includes(ent.entity)) {
let obj = {
entityName: ent.entity,
entityColor: Util.getRandomColor()
}
uniqueEnts.push(obj)
obj = null
}
})
})
return _uniqueEnts.entityName.sort().value()
// earlier tryout --> return _(uniq(uniqueEnts.entityName)).sort().value()
},
UPDATED WITH LATEST TRYOUT:
uniqueEntities() {
let uniqueEntityObj
var uniqueEntity = Array.from(new Set(data.map(el => this.nluDataUnfiltered.entities[0].entity)));
uniqueEntityObj = uniqueEntity.map(el => {
entityName: el,
entityColor: Util.getRandomColor()
});
return uniqueEntityObj
},