copy array(from obj) with no dupes
for S.O. question
by trentHarlem
JavaScript
const colors = ['red', 'green', 'green', 'red', 'blue', 'red', 'blue']
const colorsArrs = [['red', 'green'], ['green', 'red'], ['blue', 'red']]
function copyArrayWithNoDupes(array) {
return Array.from(new Set([...array.flat()])).sort();
}
console.log(colors)
//console.log(copyArrayWithNoDupes(colors))
//console.log(copyArrayWithNoDupes(colorsArrs))
const noDupes = (array) => Array.from(new Set([...array.flat()])).sort();
console.log(noDupes(colorsArrs))
const myObj = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
const myArray = [
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
]
//const allKeys = Object.keys(myObj)
//const allValues = Object.values(myObj)
const allTags = Object.values(myObj).flatMap(obj=>obj.tags)
const noDupes = (array) => Array.from(new Set([...array.flat()])).sort();
const allTags2 = myArray.map(obj => obj.tags)
//console.log(allKeys)
//console.log(allValues)
console.log(allTags)
console.log(noDupes(allTags))
console.log(allTags2)