简体   繁体   English

比较数组中的元素

[英]Compare elements in a array

I am developing an angular application and here I want to compare product quantities contains in the following prductpath array.我正在开发一个角度应用程序,在这里我想比较以下 prductpath 数组中包含的产品数量。 As an example, I want to compare array[0].qty with array[1].qty and array[1].qty with array[2].qty .例如,我想将array[0].qtyarray[1].qty以及array[1].qtyarray[2].qty Array length should not be exceeded.不应超过数组长度。

 [

  0:{qty:120}

  1:{qty:110}

  2:{qty:130}

]

How can I do it?我该怎么做?

You can use for loop to compare the object's property one by one.您可以使用for循环来一一比较对象的属性。

The following example shows how to find the largest qty :以下示例显示了如何查找最大qty

 var elArr = [{qty:120},{qty:110},{qty:130}]; var largestQty = elArr[0].qty; for(let i = 1; i<elArr.length; i++){ if(elArr[i].qty > largestQty) largestQty = elArr[i].qty; } console.log(largestQty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM