如何在没有切片,拼接的情况下删除对象元素数组
how can remove array of object element without slice, splice
我正在尝试从下面的数组对象中删除单个对象。我试过 slice 但有时它不起作用。这就是为什么我想尝试不同的方法。
{
id: "6252de4b27082fd83b94e3f4",
options: [
{ title: "extra Tomato", price: 2 },
{ title: "ketchup", price: 1 },
{ title: "medium", price: 3 },
],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery",
}
假设我想删除 medium
对象,我的预期输出是:
{
id: "6252de4b27082fd83b94e3f4",
options: [
{ title: "extra Tomato", price: 2 },
{ title: "ketchup", price: 1 },
],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery",
}
您可以尝试使用Array.filter
方法。只需提供您要删除的标题,您就会得到已删除的数组。这样做,我们不会改变原始数组,而是创建一个新数组并删除值。
const obj = {
id: '6252de4b27082fd83b94e3f4',
options: [
{ title: 'extra Tomato', price: 2 },
{ title: 'ketchup', price: 1 },
{ title: 'medium', price: 3 },
],
price: 5.1,
quantity: 1,
title: 'Carroll Montgomery',
}
obj.options = obj.options.filter((item) => item.title !== 'medium')
console.log(obj)
filter
删除您不想要的项目。
const obj = {
id: '6252de4b27082fd83b94e3f4',
options: [
{title: 'extra Tomato', price: 2},
{title: 'ketchup', price: 1},
{title: 'medium', price: 3}
],
price: 5.1,
quantity: 1,
title: 'Carroll Montgomery'
};
function removeItem(obj, item) {
// Destructure the options from the object
const {options, ...rest } = obj;
// Return a new object with a filtered
// array of options
return {
...rest,
options: options.filter(el => {
return el.title !== item;
})
};
}
console.log(removeItem(obj, 'medium'));
在这里你应该只写自定义切片函数;
var data = {id: "6252de4b27082fd83b94e3f4",
options: [
{title: 'extra Tomato', price: 2},
{title: 'ketchup', price: 1},
{title: 'medium', price: 3}],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery"};
data["options"] = data.options.filter(function(value, index, arr){
return value.title != "medium";
});
console.log(data);
您可以使用 arrays.filter(....)
从数组中删除项目
obj.options = obj.options.filter((data) => data.title !== 'medium' || data.title !== 'ketchup')
console.log(obj)
我正在尝试从下面的数组对象中删除单个对象。我试过 slice 但有时它不起作用。这就是为什么我想尝试不同的方法。
{
id: "6252de4b27082fd83b94e3f4",
options: [
{ title: "extra Tomato", price: 2 },
{ title: "ketchup", price: 1 },
{ title: "medium", price: 3 },
],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery",
}
假设我想删除 medium
对象,我的预期输出是:
{
id: "6252de4b27082fd83b94e3f4",
options: [
{ title: "extra Tomato", price: 2 },
{ title: "ketchup", price: 1 },
],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery",
}
您可以尝试使用Array.filter
方法。只需提供您要删除的标题,您就会得到已删除的数组。这样做,我们不会改变原始数组,而是创建一个新数组并删除值。
const obj = {
id: '6252de4b27082fd83b94e3f4',
options: [
{ title: 'extra Tomato', price: 2 },
{ title: 'ketchup', price: 1 },
{ title: 'medium', price: 3 },
],
price: 5.1,
quantity: 1,
title: 'Carroll Montgomery',
}
obj.options = obj.options.filter((item) => item.title !== 'medium')
console.log(obj)
filter
删除您不想要的项目。
const obj = {
id: '6252de4b27082fd83b94e3f4',
options: [
{title: 'extra Tomato', price: 2},
{title: 'ketchup', price: 1},
{title: 'medium', price: 3}
],
price: 5.1,
quantity: 1,
title: 'Carroll Montgomery'
};
function removeItem(obj, item) {
// Destructure the options from the object
const {options, ...rest } = obj;
// Return a new object with a filtered
// array of options
return {
...rest,
options: options.filter(el => {
return el.title !== item;
})
};
}
console.log(removeItem(obj, 'medium'));
在这里你应该只写自定义切片函数;
var data = {id: "6252de4b27082fd83b94e3f4",
options: [
{title: 'extra Tomato', price: 2},
{title: 'ketchup', price: 1},
{title: 'medium', price: 3}],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery"};
data["options"] = data.options.filter(function(value, index, arr){
return value.title != "medium";
});
console.log(data);
您可以使用 arrays.filter(....)
从数组中删除项目
obj.options = obj.options.filter((data) => data.title !== 'medium' || data.title !== 'ketchup')
console.log(obj)