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
CodePudding user response:
You could use 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);
CodePudding user response:
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]