Reputation: 532
Hey I am making a food delivery app, and I have a list of data. I want to remove the item if the condition matches:
Here is my data
state: [
{
id: 1,
quantity: 4,
price: 120,
subDataId: 1,
itemTotalPrice: 480
},
{
id: 1,
quantity: 5,
price: 70,
subDataId: 2,
itemTotalPrice: 350
},
{
id: 2,
quantity: 3,
price: 140,
subDataId: 1,
itemTotalPrice: 420
},
{
id: 2,
quantity: 5,
price: 80,
subDataId: 2,
itemTotalPrice: 400
},
{
id: 3,
quantity: 6,
price: 60,
itemTotalPrice: 360
}
],
Here is my code
let data = {id: 1, subDataId: 2};
// let data ={id: 3}
if (data.subDataId) {
const filtered = state.filter((filterItem) => {
return (
filterItem.id !== data.id && filterItem.subDataId !== data.subDataId
); });
if (filtered.length !== 0) {
return (state = filtered);
} else {
return [...state];
}
} else {
const filtered = state.filter((filterItem) => {
return filterItem.id !== data.id;
});
if (filtered.length !== 0) {
return (state = filtered);
} else {
return [...state];
}
}
The code does not work properly and remove several items instead of one.
EDIT Guys, Guys I want to check if the id and subdDataId matches then remove the item and return an array with the rest of the data and if only the idmatches then do the same.
Upvotes: 0
Views: 119
Reputation: 3782
filterItem.id !== data.id && filterItem.subDataId !== data.subDataId
is not correct. You want to remove the item if the id and subDataId matches, i.e. filterItem.id === data.id && filterItem.subDataId === data.subDataId
. Therefore, you want to keep the item if !(filterItem.id === data.id && filterItem.subDataId === data.subDataId)
const state = [
{
id: 1,
quantity: 4,
price: 120,
subDataId: 1,
itemTotalPrice: 480
},
{
id: 1,
quantity: 5,
price: 70,
subDataId: 2,
itemTotalPrice: 350
},
{
id: 2,
quantity: 3,
price: 140,
subDataId: 1,
itemTotalPrice: 420
},
{
id: 2,
quantity: 5,
price: 80,
subDataId: 2,
itemTotalPrice: 400
},
{
id: 3,
quantity: 6,
price: 60,
itemTotalPrice: 360
}
];
function filter(data) {
if (data.subDataId) {
const filtered = state.filter((filterItem) =>
!(filterItem.id === data.id && filterItem.subDataId === data.subDataId));
if (filtered.length !== 0) {
return filtered;
} else {
return [...state];
}
} else {
const filtered = state.filter((filterItem) => {
return filterItem.id !== data.id;
});
if (filtered.length !== 0) {
return filtered;
} else {
return [...state];
}
}
}
console.log(filter({id:1, subDataId:2}));
console.log(filter({id:1}));
Upvotes: 0
Reputation: 111
It will check if id and subDataId matches with data or just id matches with data.
const filteredArary = state.filter(item => !((item.id === data.id && item.subDataIdd === data.subDataId) || item.id === data.id))
Upvotes: 0
Reputation: 145
This is the solution I came up with from your description. You can run the snippet to check the result.
To filter items with subDataId
you have to use ||
instead of &&
.
const state = [
{
id: 1,
quantity: 4,
price: 120,
subDataId: 1,
itemTotalPrice: 480,
},
{
id: 1,
quantity: 5,
price: 70,
subDataId: 2,
itemTotalPrice: 350,
},
{
id: 2,
quantity: 3,
price: 140,
subDataId: 1,
itemTotalPrice: 420,
},
{
id: 2,
quantity: 5,
price: 80,
subDataId: 2,
itemTotalPrice: 400,
},
{
id: 3,
quantity: 6,
price: 60,
itemTotalPrice: 360,
},
];
function filterItem(data, itemToRemove) {
let filteredData = [...data];
// we are checking if itemToRemove has subDataId
if (itemToRemove.subDataId) {
// we are only taking an item if it's id or subDataId doesn't match with itemToRemove's id or subDataId
filteredData = data.filter(
(item) =>
item.id !== itemToRemove.id || item.subDataId !== itemToRemove.subDataId
);
} else {
// if itemToRemove doesn't have subDataId we are only taking an item if it's id doesn't match with itemToRemove's id
filteredData = data.filter((item) => item.id !== itemToRemove.id);
}
return filteredData;
}
const item1 = { id: 1};
const item2 = { id: 1, subDataId: 1 };
console.log('Without subDataId', filterItem(state, item1));
console.log('With subDataId', filterItem(state, item2));
Upvotes: 0
Reputation: 9310
I guess you are overthinking the problem a little bit. Why not use a simple filter()
? In your explanation condition 2 is true
as soon as condition 1 is true
. So the actual condition is:
id
&& subDataId
must match the values to be removed.Try this:
const data = [{
id: 1,
quantity: 4,
price: 120,
subDataId: 1,
itemTotalPrice: 480,
},
{
id: 1,
quantity: 5,
price: 70,
subDataId: 2,
itemTotalPrice: 350,
},
{
id: 2,
quantity: 3,
price: 140,
subDataId: 1,
itemTotalPrice: 420,
},
{
id: 2,
quantity: 5,
price: 80,
subDataId: 2,
itemTotalPrice: 400,
},
{
id: 3,
quantity: 6,
price: 60,
itemTotalPrice: 360,
},
];
const result = data.filter((item) => item.id !== 1 && item.subDataId !== 2);
console.log(result);
Upvotes: 0
Reputation: 154
Try this,
let newState = state.filter((item) =>{
return item.id !== data.id && item.subDataId !== data.subDataId;
});
Upvotes: 2