简体   繁体   English

查找并返回Javascript数组中的完全匹配项(重复项)

[英]Find and return exact matches (duplicates) in Javascript Array

So I've been googling a lot and tried to use different filters for arrays and such, to no avail. 因此,我一直在大量搜索,并尝试对数组使用不同的过滤器,但无济于事。

I have an array with a string like so: 我有一个像这样的字符串数组:

var foo = ['1X31UX11','X3U11X1','33X11U12'];

Is there a way to check every string against each other in the array, and if there's an exact match (ie the exact same order of characters) it prints how many times the particular string occurs & also prints the string in question? 有没有一种方法可以检查数组中每个字符串是否相互匹配,并且如果存在完全匹配(即完全相同的字符顺序),它将打印出特定字符串出现了多少次并还打印了相关字符串?

 function arrayRepeats(array) { var returnObject = {}; for (var i = 0; i < array.length; i++) { if (returnObject[array[i]]) { returnObject[array[i]]++; } else { returnObject[array[i]] = 1; } } return returnObject; } var foo = ['1X31UX11','X3U11X1','33X11U12']; console.log(arrayRepeats(foo)); var bar = [1, 1, 2, 1, 3, 1, 31, 3, 1, 5, 4, 1]; console.log(arrayRepeats(bar)); 

Use Array#reduce to collect the the number of appearances of each string into an object: 使用Array#reduce收集每个字符串出现在一个对象中的数量:

 var foo = ['1X31UX11','X3U11X1','33X11U12', '1X31UX11']; var result = foo.reduce(function(count, str) { count[str] = (count[str] || 0) + 1; return count; }, {}); console.log(result); 

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

相关问题 javascript - 在数组上使用过滤器和匹配方法来查找完全匹配 - javascript - use the filter and match methods on an array to find exact matches 在数组JavaScript中查找重复项 - Find duplicates in array JavaScript JavaScript 如何在不提供与 object 值匹配的确切搜索值的情况下查找 object 数组的数据 - JavaScript how to find data of array of object without giving exact search value that matches with value of object Javascript从2个数组中返回数组,删除重复项 - Javascript return array from 2 arrays removing duplicates 在JavaScript中找到不同的匹配项和度假胜地数组 - find different matches and resort array in javascript 在 javascript 中查找具有重复项的数组中的第二大元素 - Find second largest elements in array with duplicates in javascript 使用javascript中的不同键在“对象数组”中查找重复项 - Find duplicates in "Array of objects" with different keys in javascript 在 JavaScript 数组中查找重复项并更新其值 - Find duplicates and update their values in JavaScript array 根据javascript中的匹配条件从数组数组中查找匹配项 - Find matches from an array of arrays based on matches condition in javascript 如果在javascript中找到匹配项,则搜索索引数组并返回完整数组 - Search indexed array and return full array if matches found in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM