简体   繁体   English

比较两个数组并计数事件

[英]Compare two arrays and count incidents

I have two array我有两个数组

$array1=('18753933','18753933','18771982')
$array2=('18753933','18771982')

I'm going through each one to compare the values that are the same in each array我正在逐个比较每个数组中相同的值

var contarArticulosCargados=0;

for(var $i=0;$i<$array1.length;$i++) {
    for(var $j=0;$j<$array2.length;$j++) {
        if($array1[$i]===$array[$j]) {
            countArticulosCargados++;
        }
    }
}

console.log(countArticlesLoaded);

This example that I have put in my code works well, that is to say, I am doing the comparison and it works without problem.我放在代码中的这个例子运行良好,也就是说,我正在做比较并且它没有问题。

What do I want to do?我想做什么? (It's what I have no idea how to do) (这是我不知道该怎么做)

As you can see in $array1 is the value 18753933 and in $array2 is also the value 18753933 .正如您在$array1看到的值 18753933 而在$array2中也是值18753933

What I want is that when the value is repeated this is counted, in the $array1 the value 18753933 is 2 times and the value 18771982 is 1 time.我想要的是,当值重复时,这被计算在内,在$array1 ,值187539332次,值187719821次。

I want to see something like this:我想看到这样的事情:

18753933 repeats: 2 
18771982 is repeated: 1

could you give me an idea of what I'm missing?你能告诉我我错过了什么吗? I always find it hard to make comparisons between arrays.我总是发现很难在数组之间进行比较。 Thank you.谢谢你。

Simply map over array2 and take the length of the filtered array1.只需映射 array2 并获取过滤后的 array1 的长度。

 const a1 = ['18753933','18753933','18771982']; const a2 = ['18753933','18771982']; const res = a2.map(e => ({[e]: a1.filter(k => k === e).length})); console.log(res);

To remove elements that were not found, you could filter first要删除未找到的元素,您可以先过滤

a2.filter(e => a1.includes(e)).map(e => ({[e]: a1.filter(k => k === e).length}))

or after或之后

a2.map(e => ({[e]: a1.filter(k => k === e).length})).filter(e => Object.values(e).every(x => x > 0));

Or use reduce或使用减少

 const a1 = ['18753933','18753933','18771982']; const a2 = ['18753933','18771982']; const res = a2.reduce((a, b) => { const l = a1.filter(e => e === b).length; if (l) a[b] = l; return a; }, {}); console.log(res);

What it looks like you'll need to do is create an Array of Objects.看起来您需要做的是创建一个对象数组。 Something like:就像是:

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
</head>

<body>

  <h2>JavaScript Array of Objects</h2>

  <p id="demo"></p>

  <script>
    var array1 = ['18753933', '18753933', '18771982'];
    var array2 = ['18753933', '18771982'];
    var contarArticulosCargados = [];

    // for each of the numbers in array2
    for (var i = 0; i < array2.length; i++) {

      // create an object
      contarArticulosCargados.push({
        articulos: array2[i],
        contar: 0
      });

      // for each of the numbers in array2
      for (var j = 0; j < array1.length; j++) {

        if (array2[i] === array1[j]) {
          // increment counter for the object
          var foo = contarArticulosCargados.length - 1;
          contarArticulosCargados[foo].contar++;

        }
      }
    }

    // create string for result
    var result = "";
    for (var k = 0; k < contarArticulosCargados.length; k++) {
      result += contarArticulosCargados[k].articulos + " repeats: " + contarArticulosCargados[k].contar + ". ";
    }

    // send result to html
    document.getElementById("demo").innerHTML = result;
    // or console as string
    console.log(result);
    // or console as array
    console.log(contarArticulosCargados);

  </script>
</body>

</html>

You can copy & paste the html to test, to see the results.您可以复制并粘贴 html 进行测试,以查看结果。

You might want to switch array1 & array2, but I think I have them the right way around (from your example).您可能想切换 array1 和 array2,但我认为我有正确的方法(从您的示例中)。

Change array1 to something else might help visualisation将 array1 更改为其他内容可能有助于可视化

var array1 = ['18753933', '18753933', '18771982', '18771982', '18771982', '18771982', '18771982', '18771982', '18771982'];

If array1 was as above, the output is:如果 array1 如上所述,则输出为:

18753933 repeats: 2. 18771982 repeats: 7. 

Or if array1 is as originally stated in the question, the output is:或者,如果 array1 与问题中最初陈述的一样,则输出为:

18753933 repeats: 2. 18771982 repeats: 1. 

But, of course the question tags are JavaScript & jQuery but the code is PHP .但是,当然问题标签是JavaScriptjQuery但代码是PHP So I'm wrong either way.所以我错了。

 const a1 = ['18753933','18753933','18771982']; const a2 = ['18753933','18771982']; const r = {}; a2.forEach(_ => r[_] = 0) a1.forEach(_ => { if (r[_] === 0 || r[_]) { r[_] += 1 } }) console.log(r)

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

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