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);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter