0

I have an array of objects - lists[]; and an object - obj;

This is the lists array:

{
  _id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
  name: 'list1',
  content: [ 'aaaa' ],
  description: 'aa',
  tags: [],
  lastmodified: 1,
  __v: 0
},{
  _id: new ObjectId("61840def80a88d1b2ffce400"),
  name: 'list',
  content: [ 'list!' ],
  description: 'test',
  tags: [],
  __v: 0
}

and this is the obj object:

{
  _id: new ObjectId("61840def80a88d1b2ffce400"),
  name: 'list',
  content: [ 'list!' ],
  description: 'test',
  tags: [],
  __v: 0
}

Simple Question: How do I delete the object from "lists", similar to the "obj" one

1

2 Answers 2

1

You could use the JavaScript Array.filter() method to exclude the target object from the list:

const list = [{
_id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
  name: 'list1',
  content: [ 'aaaa' ],
  description: 'aa',
  tags: [],
  lastmodified: 1,
  __v: 0
},{
  _id: new ObjectId("61840def80a88d1b2ffce400"),
  name: 'list',
  content: [ 'list!' ],
  description: 'test',
  tags: [],
  __v: 0
}];

const targetObj = {
  _id: new ObjectId("61840def80a88d1b2ffce400"),
  name: 'list',
  content: [ 'list!' ],
  description: 'test',
  tags: [],
  __v: 0
};

const filteredList = list.filter((element) => element._id !== targetObj._id);
Sign up to request clarification or add additional context in comments.

Comments

0

use some sort of filtering... heres an idea.

items = [{
  _id: new ObjectId("61840c5ce237f22a1c7a1ac7"),
  name: 'list1',
  content: [ 'aaaa' ],
  description: 'aa',
  tags: [],
  lastmodified: 1,
  __v: 0
},{
  _id: new ObjectId("61840def80a88d1b2ffce400"),
  name: 'list',
  content: [ 'list!' ],
  description: 'test',
  tags: [],
  __v: 0
}]

removeID = "61840def80a88d1b2ffce400"

items = [item for item in items if item['id'] != removeID]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.