Home > Software engineering >  Compare two arrays where show if an element is greater in an array Javascript
Compare two arrays where show if an element is greater in an array Javascript

Time:09-07

I have two arrays of objects. I need to check if the element called quantity is larger in one of the two arrays. Example:

Array detail:

[
  {
    provider: { uid: '6271a32082193f4b88e292f0', name: 'Genérico' },
    _id: '628ff19af062bde6a9fd7a3b',
    name: 'MALBORO ARTESANAL "20"',
    quantity: 6,
    subtotal: 3156,
    total: 0,
    quantityOnBd: 6
  },
  {
    provider: { uid: '6271a32082193f4b88e292f0', name: 'Genérico' },
    _id: '628fef9bf062bde6a9fd7986',
    name: 'MALBORO ROJO "20"',
    quantity: 7,
    subtotal: 4368,
    total: 0,
    quantityOnBd: 7
  }
]

Array sale.detail:

[
  {
    provider: { uid: '6271a32082193f4b88e292f0', name: 'Genérico' },
    _id: '628fef9bf062bde6a9fd7986',
    name: 'MALBORO ROJO "20"',
    quantity: 7,
    subtotal: 4368,
    total: 0
  },
  {
    provider: { uid: '6271a32082193f4b88e292f0', name: 'Genérico' },
    _id: '628ff19af062bde6a9fd7a3b',
    name: 'MALBORO ARTESANAL "20"',
    quantity: 6,
    subtotal: 3156,
    total: 0
  }
]

I already have a code in which I use two FOR to know if at a certain moment the quantity is greater in one of the two, it works but I would like to know if there is a better way to do it that is more efficient.

    for (let i = 0; i < detail.length; i  ) {
        for (let j = 0; j < sale.detail.length; j  ) {
            if (detail[i]['_id'] === sale.detail[j]['_id']) {
                if (detail[i]['quantity'] > sale.detail[j]['quantity']) {
                    return res.status(400).json({msg:'No puedes regresar mas de la cantidad original'});
                }
            }
        }
    }

CodePudding user response:

Something like this:

const map = new Map();
for (const item of detail) {
  let tuple = map[item._id]
  if (!tuple) {
    tuple = {};
    map.set( item._id, tuple ) ;
  }
  tuple.item  = item;
}
for (const sale of sale.detail) {
  let tuple = map[sale._id]
  if (!tuple) {
    tuple = {};
    map.set( sale._id, tuple );
  }
  tuple.sale = sale;
}

for (const [id,{item,sale}] of map.entries() {
  if (!item && sale) {
    console.log(`id:${id} : sale without item`);
  } else if ( item && !sale ) {
    console.log(`id:${id} : item without sale`);
  } else if (item && sale) {
    switch ( Math.sign( item.quantity - sale.quantity ) ) {
    case  0 : console.log(`id:${id} : sale.quantity === item.quantity`); break;
    case  1 : console.log(`id:${id} : item.quantity > sale.quantity`  ); break;
    case -1 : console.log(`id:${id} : item.quantity < sale.quantity`  ); break;
    default:
      throw new Error('unreachable code reached')
    }
  } else {
    throw new Error("unreachable code reached: no sale and no item");
  }
}

CodePudding user response:

you can save any object in Object use _id in object key, such as

{
  "628ff19af062bde6a9fd7a3b":{
      provider: { uid: '6271a32082193f4b88e292f0', name: 'Genérico' },
      _id: '628ff19af062bde6a9fd7a3b',
      name: 'MALBORO ARTESANAL "20"',
      quantity: 6,
      subtotal: 3156,
      total: 0,
      quantityOnBd: 6
  }
}

full code

const map = {};

detail.forEach(e=>{
  map[e._id] = e;
});

const hasLarger = sale.detail.some(e=>{
  const { _id , quantity:saleQuantity } = e;
  const detailQuantity = map[_id].quantity;
  if(detailQuantity>saleQuantity){
    return true;
  }
  return false;
});
if(hasLarger){
  return res.status(400).json({msg:'No puedes regresar mas de la cantidad original'});
}
  • Related