[Vuejs]-Filter value of object in VueJS

1๐Ÿ‘

โœ…

  1. I think the problem is in id = 0 + id. When id is 5 this line evaluates to 5 only not 05. Change to id = '0' + id, which will convert to string.

  2. After the filter, use the map 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());

Leave a comment