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

187
Views
Remove an object from an object array using index?

I want to remove 3rd object from an array in which the color is red. I want to remove it using the index as some objects may not have an _id attribute and other attributes are not unique among them.

[
  {  color: "BLUE"
     imageURL: "/uploads/image-1639632524718.png"
     inStock: 4
     _id: "61b9bff23b027548ed2f737e"
  },
  {  color: "green"
     imageURL: "/uploads/image-1639561204805.png"
     inStock: 6
     _id: "61ba098f3b027548ed2f737f"
  },
  {  color: "Red"
     imageURL: "/uploads/image-1639647424471.png"
     inStock: 6
  },
  {  color: "Star Light"
     imageURL: "/uploads/image-1639650244179.png"
     inStock: 60
  }
]  

After removing the object array will look like.

[
  {  color: "BLUE"
     imageURL: "/uploads/image-1639632524718.png"
     inStock: 4
     _id: "61b9bff23b027548ed2f737e"
  },
  {  color: "green"
     imageURL: "/uploads/image-1639561204805.png"
     inStock: 6
     _id: "61ba098f3b027548ed2f737f"
  },
  {  color: "Star Light"
     imageURL: "/uploads/image-1639650244179.png"
     inStock: 60
  }
]  

How can I do this?

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

0

this splice function does that

someArray.splice(index, 1)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

about 4 years ago · Juan Pablo Isaza Report

0

Use Array.splice() method to remove item from specified position.

const list = [
  {  color: "BLUE",
     imageURL: "/uploads/image-1639632524718.png",
     inStock: 4,
     _id: "61b9bff23b027548ed2f737e"
  },
  {  color: "green",
     imageURL: "/uploads/image-1639561204805.png",
     inStock: 6,
     _id: "61ba098f3b027548ed2f737f"
  },
  {  color: "Red",
     imageURL: "/uploads/image-1639647424471.png",
     inStock: 6
  },
  {  color: "Star Light",
     imageURL: "/uploads/image-1639650244179.png",
     inStock: 60
  }
];

const remove = (arr, index) => arr.splice(index, 1);
remove(list, 2);
console.log("list after removal: ", list);

about 4 years ago · Juan Pablo Isaza Report

0

(Once you've fixed your data structure - it's not an array, and you're missing commas...)

You can filter out all those objects that aren't located at that index.

const data=[{color:"BLUE",imageURL:"/uploads/image-1639632524718.png",inStock:4,_id:"61b9bff23b027548ed2f737e"},{color:"green",imageURL:"/uploads/image-1639561204805.png",inStock:6,_id:"61ba098f3b027548ed2f737f"},{color:"Red",imageURL:"/uploads/image-1639647424471.png",inStock:6},{color:"Star Light",imageURL:"/uploads/image-1639650244179.png",inStock:60}];

const out = data.filter((obj, i) => i !== 2);

console.log(out);

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!