I have array like this
let quotes = [
{
quoteNumber :'001',
items : [
{
partnumber : '5551',
supplierQty : 0,
stock : 5
},
{
partnumber : '5552',
supplierQty : 1,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
},
{
quoteNumber : '002',
items : [
{
partnumber : '5554',
supplierQty : 1,
stock : 0
},
{
partnumber : '5552',
supplierQty : 5,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
},
{
quoteNumber : '003',
items : [
{
partnumber : '5554',
supplierQty : 0,
stock : 1
},
{
partnumber : '5552',
supplierQty : 0,
stock : 3
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
}
]
the result that I want is remove items inside quote that has supplierQty === 0 and remove quote that every items in it has supplierQty === 0
this is the result that I want.
let quotes = [
{
quoteNumber :'001',
items : [
{
partnumber : '5552',
supplierQty : 1,
stock : 0
}
]
},
{
quoteNumber : '002',
items : [
{
partnumber : '5554',
supplierQty : 1,
stock : 0
},
{
partnumber : '5552',
supplierQty : 5,
stock : 0
}
]
}
]
this is my code
let results = quotes.filter((quote , index) => {
let filterQuote = quote.items.filter((item) => {
return item.supplierQty > 0;
})
return filterQuote.length > 0;
})
console.log(results);
but It just remove quote 003 only . It didn't remove items inside quote 001 and 002 .
How can I do that . thank for help
results = [
{
quoteNumber :'001',
items : [
{
partnumber : '5551',
supplierQty : 0,
stock : 5
},
{
partnumber : '5552',
supplierQty : 1,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
},
{
quoteNumber : '002',
items : [
{
partnumber : '5554',
supplierQty : 1,
stock : 0
},
{
partnumber : '5552',
supplierQty : 5,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
}
]
You need to filter items first and remove the quote which does not contain any item using Array.filter.
const quotes = [
{
quoteNumber :'001',
items : [
{
partnumber : '5551',
supplierQty : 0,
stock : 5
},
{
partnumber : '5552',
supplierQty : 1,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
},
{
quoteNumber : '002',
items : [
{
partnumber : '5554',
supplierQty : 1,
stock : 0
},
{
partnumber : '5552',
supplierQty : 5,
stock : 0
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
},
{
quoteNumber : '003',
items : [
{
partnumber : '5554',
supplierQty : 0,
stock : 1
},
{
partnumber : '5552',
supplierQty : 0,
stock : 3
},
{
partnumber : '5553',
supplierQty : 0,
stock : 2
}
]
}
];
const results = quotes.map(({ items, ...quote }) => ({
...quote,
items: items.filter(({ supplierQty }) => supplierQty > 0)
})).filter(({ items }) => items.length > 0);
console.log(results);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With