Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
How to delete a Javascript object element?

I'm using the following code to update any given property inside a JS object:

var main = {
          items: [
                  {
                    "name": "T-Shirt",
                    "description": "Green XL",
                    "quantity": "1",
                  },
                  {
                    "name": "Shoes",
                    "description": "Running, Size 10.5",
                    "quantity": "2",
                  }
                ]
        };

let project = main.items.find((p) => {
    return p.name === 'Shoes';
});

project.quantity = '3';
console.log(main); //shoes quantity is now 3

And it works, however, i want it to also be able to delete the whole project variable (for example, the whole Shoes block). I tried:

let project = main.items.find((p) => {
    return p.name === 'Shoes';
});
delete project;

console.log(main); //shoes still exist

But it doesn't work. What would be the best way to delete the property?

Edit: My main issue here was about finding the index of the element to be deleted. The chosen answer below provides the solution.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Array#filter.

main.items = main.items.filter(p => p.name !== 'Shoes');
about 4 years ago · Juan Pablo Isaza Report

0

You cannot use the delete operator like this. To remove the found item from the array in-place, you will want to use Array#splice()

const foundIndex = main.items.findIndex((p) => {
  return p.name === 'Shoes';
});

if (foundIndex !== -1) main.items.splice(foundIndex, 1)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!