In this example we will see how to search object by id and remove it from json array in javascript. we need to add JavaScript function that takes in one such array as the first argument and id string in second argument for search and remove object from json array.
After that this function search for the object by that id, and if the array contains that object, we should remove object from json array in javascript.So, let's learn how to remove object from json array in javascript.
Example :
const arr = [
{id: "1", name: "car", type: "vehicle"},
{id: "2", name: "bike", type: "vehicle"},
{id: "3", name: "cycle", type: "vehicle"},
{id: "4", name: "pink", type: "color"},
{id: "5", name: "blue", type: "color"},
{id: "6", name: "red", type: "color"},
];
const removeById = (arr, id) => {
const requiredIndex = arr.findIndex(el => {
return el.id === String(id);
});
if(requiredIndex === -1){
return false;
};
return !!arr.splice(requiredIndex, 1);
};
removeById(arr, 5);
console.log(arr);
Output :
[
{id: "1", name: "car", type: "vehicle"},
{id: "2", name: "bike", type: "vehicle"},
{id: "3", name: "cycle", type: "vehicle"},
{id: "4", name: "pink", type: "color"},
{id: "6", name: "red", type: "color"},
]
In this artical, we will explain the laravel 9 file upload example step by step. As we know file upload is the most comm...
Mar-11-2022
Hey fellow Ubuntu enthusiasts! 🐧✨ If you're looking to step into the awesome world of Node.js and npm for your web d...
Jan-12-2024
In this example we can see how to add multiple dummy records in the database at a time using tinker and factory, mo...
May-21-2020
In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a...
Mar-31-2023