How to Search Object by ID and Remove It from JSON Array In JavaScript

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);
[
{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"},
]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.