What is a good way to iterate over a js array of objects, and then within each object, check a value in the object against all others for uniqueness, and push to a new array.
I have an array of objects:
const weatherArray = [
{
dt: 1526871600
dt_txt: "2018-05-21 03:22:00"
},
{
dt: 1526871600
dt_txt: "2018-05-22 03:30:00"
},
{
dt: 1526871600
dt_txt: "2018-05-21 03:50:00"
},
{
dt: 1526871600
dt_txt: "2018-05-23 03:17:00"
},
{
dt: 1526871600
dt_txt: "2018-05-23 03:23:00"
}
]
I need to check each object and if the dt_txt (just date, not time eg: 2018-05-23, 2018-05-21 etc) is unique, push that object into a new array.
Below is what I have been trying, I commented to the code to show my train of thought.
var uniqueDays = []
function getDays(weatherArray) {
// push first value to new array to compare against other items
uniqueDays.push(weatherArray[0])
// get just the yyyy-mm-dd from txt we're comparing against
let firstDayString = weatherArray[0].dt_txt.split(" ")[0]
weatherArray.map((day) => {
let dayString = day.dt_txt.split(" ")[0]
uniqueDays.map((uniqueDay, index) => {
// get just the yyyy-mm-dd for new array items
let unqiueDayString = uniqueDay.dt_txt.split(" ")[0]
// if the value exists, do nothing
if (unqiueDayString == dayString) {
console.log('duplicate');
} else {
// otherwise push to new array (this is the issue)
uniqueDays.push(day)
}
})
})
return uniqueDays
}
The issue I am having is that pushing to unique day's within it's own map function is causing a recursion issue. I know there has to be a better way to this. Any help or direction would be a big relief, I've been struggling with this for some time.