如何在javascript中区分数组值

如何在javascript中区分数组值,javascript,arrays,object,Javascript,Arrays,Object,我有数组listA和listB,使用javascript比较和计算每个列表的输出 listA=[1, 3, 5] listB=[3, 3, 2] if listA[0] > listB[0] then x gets the point 1 if listA[1]==listB[1] then x and y no points if listA[2] > listB[2] then y gets the point 1 function getPoints(x, y){ v

我有数组listA和listB,使用javascript比较和计算每个列表的输出

listA=[1, 3, 5]
listB=[3, 3, 2] 

if listA[0] > listB[0] then x gets the point 1
if listA[1]==listB[1] then x and y no points
if listA[2] > listB[2] then y gets the point 1

function getPoints(x, y){
  var pointX, pointY =0
  var result = x.forEach(e=>{
   e[0] > y[0] ?  pointX++ : e[1] < y[1] pointY ++
 })
}

console.log(this.getPoints(listA, listB));
Expected Output

1 1
listA=[1,3,5]
listB=[3,3,2]
如果listA[0]>listB[0],则x得到点1
如果listA[1]==listB[1],则x和y没有点
如果listA[2]>listB[2],则y得到点1
函数getPoints(x,y){
变量pointX,pointY=0
var结果=x.forEach(e=>{
e[0]>y[0]?pointX++:e[1]

如何使用javascript,陷入困境,

我知道你想要什么了

但我建议你稍微改变一下

所以脚本应该是这样的

 <script>
  const listA = [1, 3, 5];
  const listB = [3, 3, 2];

  // if listA[0] > listB[0] then x gets the point 1
  // if listA[1]==listB[1] then x and y no points
  // if listA[2] > listB[2] then y gets the point 1

  const point = { a: 0, b: 0 };
  function getPoints(x, y) {
    x.forEach((item, index) => {
      if (item > y[index]) {
        point.a++;
      }
      if (item < y[index]) {
        point.b++;
      }
    });
  }
  this.getPoints(listA, listB);
  console.log(point);
</script>

常数listA=[1,3,5];
常量listB=[3,3,2];
//如果listA[0]>listB[0],则x得到点1
//如果listA[1]==listB[1],则x和y没有点
//如果listA[2]>listB[2],则y得到点1
常数点={a:0,b:0};
函数getPoints(x,y){
x、 forEach((项目、索引)=>{
如果(项目>y[索引]){
点a++;
}
如果(项目
我从函数中取出了pointA和pointB,并创建了一个包含a和b作为属性的对象。 它们将包含您通过的每个参数的总分


希望这将解决您的问题

要比较阵列,请使用以下代码:

// Warn if overriding existing method
if(Array.prototype.equals)
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

我建议像这样使用reduce

const listA=[234444,3];
const listB=[4444,33,2];
函数getWinningPointsTally(listA、listB){
返回listA.reduce((totalPoints,currentListAItem,index)=>{
如果(currentListAItem>listB[索引]){
总点数[0]++;
}else if(listB[index]>currentListAItem){
总分[1]++;
} 
返回总分;
}, [0, 0]);
}
console.log(this.getWinningPointsTally(listA,listB))
const-calcPoints=(listA,listB)=>
listA.reduce((acc,p,i)=>[acc[0]+(p>listB[i]),acc[1]+(p有很多方法可以做到这一点,但这应该简单易懂。我选择了一个常规for循环,因为您在每个数组中访问相同的索引

listA=[1,3,5]
listB=[3,3,2]
函数getPoints(x,y){
变量xPoints=0,yPoints=0;
对于(变量i=0;iy[i])xPoints++
else如果(x[i]
使用
reduce
将简化。构建[累积点X,累积点Y]

函数获取点(x,y){
返回x.reduce(
([a,b],curr,i)=>[curr>y[i]?a+1:a,y[i]>curr?b+1:b],
[0, 0]
);
}
listA=[1,3,5];
listB=[3,3,2];

log(getPoints(listA,listB))
forEach
提供第二个参数作为索引
。forEach((e,i)=>…)
。您可以使用它来访问数组的元素。
console.log中的
this
关键字不是必需的。这里有几个问题。1) 在这个场景中,您确实没有提供“比较和计算”的确切含义的正确解释。2)
Array#forEach()
没有返回3)您的函数没有返回。中断的代码通常不能很好地替代正确解释您期望代码完成的内容您的意思是“比较并计算输出”?你在比较什么?你在算什么?
[1, 2, [3, 4]].equals([1, 2, [3, 2]]) === false;
[1, 2,3].equals([1, 2, 3]) === true;
[1, 2, [3, 4]].equals([1, 2, [3, 4]]) === true;