Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS How to remove object in multi demension array?

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
       }
    ]
  }
]


like image 625
mr.StandAloneZ Avatar asked Mar 25 '26 04:03

mr.StandAloneZ


1 Answers

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);
like image 185
Derek Wang Avatar answered Mar 27 '26 17:03

Derek Wang