1๐
โ
-
I think the problem is in
id = 0 + id
. When id is 5 this line evaluates to5
only not05
. Change toid = '0' + id
, which will convert to string. -
After the
filter
, use themap
to extract id.
const events = [
{
id: "1234567",
createTimestamp: "2020",
name: {
action: "",
allDay: false,
category: "Misc",
startAt: "05",
title: "foo"
},
updateTimestamp: "2020"
}
];
function filterByHour(id) {
if (id < 10) {
id = "0" + id;
}
const result = events
.filter(item => item.name.startAt === String(id))
.map(x => x.id);
return result;
}
console.log(filterByHour(5));
console.log(filterByHour(11));
console.log(filterByHour());
Source:stackexchange.com