check an array of objects exists / check using regex

PHOTO EMBED

Sun Apr 28 2024 00:11:22 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #objects #checking #array #regex

const todos = [
  {
    id: 1,
    task: "buy milk",
  },
  {
    id: 2,
    task: "go to gym",
  },
  {
    id: 3,
    task: "clean apartment",
  },
];

const existingToDos = []; // create new array
todos.forEach((item) => todos.length && existingToDos.push(item)); // loop over and push into new arrat if the length is more than 0;

console.log("existing =>", existingToDos);





// check todos that have a task that has a string containing 'buy'
const containsBuy = [];
const reguarExp = new RegExp("buy");
todos.filter((item) => item.task.match(reguarExp) && containsBuy.push(item));
console.log("containsBuy => ", containsBuy);
content_copyCOPY