比较两个数组的每5个数字

时间:2018-03-22 14:18:56

标签: javascript arrays

var count = 0;
arrA = ["6", "2", "21", "8", "4", "12"];
arrB = ["8", "2", "12", "2", "5", "11"];

我想比较每个元素 仅当arrA[i] > arrB[i]时,则计数加1

如何在javascript中实现此功能?

4 个答案:

答案 0 :(得分:2)

您可以通过添加计算数值的结果来减少数组。



var arrA = ["6", "2", "21", "8", "4", "12"],
    arrB = ["8", "2", "12", "2", "5", "11"],
    count = arrA.reduce((c, v, i) => c + (+v > +arrB[i]), 0);
    
console.log(count);




答案 1 :(得分:0)

使用简单的for循环:

for(let i = 0; i < arrA.length; i++) {
    if (arrA[i] > arrB[i]) {
        count++;
    }
}

Alternatively, Array.forEach would also work:

arrA.forEach((value, index) => {
  if (value > arrB[index]) {
    count++;
  }
})

答案 2 :(得分:0)

您可以使用Array#every

&#13;
&#13;
public static XHeader getObjectHeader(UC4Object uc4Object) {
    XHeader header = null;
    String objectName = uc4Object.getName();
    String uc4ObjectClassName = uc4Object.getClass().getSimpleName();
    System.out.println(String.format("Object is an instance of class %s.", uc4ObjectClassName));
    switch (uc4ObjectClassName) {
        case "JobPlan":
            System.out.println(String.format("Object %s is a standard workflow.", objectName));
            header = ((JobPlan)uc4Object).header();
            break;
        case "WorkflowIF":
            System.out.println(String.format("Object %s is an IF workflow.", objectName));
            header = ((WorkflowIF)uc4Object).header();
            break;
        case "WorkflowLoop":
            System.out.println(String.format("Object %s is a FOREACH workflow.", objectName));
            header = ((WorkflowLoop)uc4Object).header();
            break;
        case "Schedule":
            System.out.println(String.format("Object %s is a Schedule.", objectName));
            header = ((Schedule)uc4Object).header();
            break;
        case "Script":
            System.out.println(String.format("Object %s is a Script.", objectName));
            header = ((Script)uc4Object).header();
            break;
        case "TimeEvent":
            System.out.println(String.format("Object %s is a Time Event.", objectName));
            header = ((TimeEvent)uc4Object).header();
            break;
        case "FileEvent":
            System.out.println(String.format("Object %s is a File Event.", objectName));
            header = ((FileEvent)uc4Object).header();
            break;
        case "DatabaseEvent":
            System.out.println(String.format("Object %s is a DB Event.", objectName));
            header = ((DatabaseEvent)uc4Object).header();
            break;
        case "ConsoleEvent":
            System.out.println(String.format("Object %s is a Console Event.", objectName));
            header = ((ConsoleEvent)uc4Object).header();
            break;
        case "Job":
            System.out.println(String.format("Object %s is an OS Job.", objectName));
            header = ((Job)uc4Object).header();
            break;
        case "FileTransfer":
            System.out.println(String.format("Object %s is a File Transfer.", objectName));
            header = ((FileTransfer)uc4Object).header();
            break;
        default:
            System.out.println(String.format("Unknown object type."));
            header = null;
    }
    return header;
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用array.prototype.reduce

&#13;
&#13;
var arrA = ["6", "2", "21", "8", "4", "12"];
var arrB = ["8", "2", "12", "2", "5", "11"];

var count = arrA.reduce((m, o, i) => m + (o > arrB[i] ? 1 : 0), 0);
console.log(count);
&#13;
&#13;
&#13;

或者array.prototype.filter

&#13;
&#13;
var arrA = ["6", "2", "21", "8", "4", "12"];
var arrB = ["8", "2", "12", "2", "5", "11"];

var count = arrA.filter((e, i) => e > arrB[i]).length;
console.log(count);
&#13;
&#13;
&#13;