-1

a function - 2 arrays : costPrice & productAndQty - trying to search all the product codes of same type in productAndQty and return a list of cost prices.

/**
 * productAndQty : subscription-id, productcode, s,m,l,xl
 * costprice: productcode, cp-s, cp-m ....... soo on!
 */
function getSupply(productAndQty,costPrice){

  /**totalQtyAsProduct is the final array */
  var totalQtyAsProduct = [];

  /**co[0] is product code */
  costPrice.forEach(function(co){
    var tempPQ = productAndQty.filter(function(po){
      return po[1]==co[0];
    });
    
    c(tempPQ);
    c(tempPQ!=[]);

    if(tempPQ!=[]){
      totalQtyAsProduct.push(calcSupply(tempPQ,costPrice));
    }

  });

  return totalQtyAsProduct;
}

but if(tempPQ!=[]) always return true; when I console.log(tempPQ), there are many [] that i dont want to pass to next function!

Navneet
  • 77
  • 6
  • 1
    Objects are never equal even if they have the same properties/values set. Arrays are a kind of object. `{} == {} // false` and `[] == [] // false`. If you want to see if an array is empty, check `.length`. – Ouroborus Jun 03 '21 at 03:19
  • Does this answer your question? [Javascript: Why are two objects not equal?](https://stackoverflow.com/questions/33299889/javascript-why-are-two-objects-not-equal) – Ouroborus Jun 03 '21 at 03:24

1 Answers1

1

Just check length of array returned by filter(). An empty array has zero length which is falsy

if(tempPQ.length)
charlietfl
  • 170,828
  • 13
  • 121
  • 150