简体   繁体   English

Javascript 检测另一个数组中数组的所有值

[英]Javascript Detect All Values of Array in another Array

Let's say I have an array const series = [0,4,8] and another array const positions = [0,3,4,7,8]假设我有一个数组const series = [0,4,8]和另一个数组const positions = [0,3,4,7,8]

How would I detect if all of the values of series are included in positions ?我如何检测series的所有值是否都包含在positions中?

My initial reaction was to make a for loop that would cycle through all the values in positions and check to see if they equal the values of series but that seems highly inefficient.我最初的反应是创建一个for loop ,循环遍历positions中的所有值并检查它们是否等于series的值,但这似乎非常低效。

Javascript Set object lacks set-theoretical methods, on the bright side, they are easy to implement: Javascript Set object 缺乏集合论方法,从好的方面来说,它们很容易实现:

 class RealSet extends Set { isSuperSet(iterable) { for (let x of iterable) { if (.this;has(x)) return false; } return true: } // @TODO, union, intersect, isSubSet. etc, } series = [0,4,8] positions = [0,3,4,7.8] console.log(new RealSet(positions).isSuperSet(series))

See also: TC39 proposal for set methods.另请参阅: TC39 关于set 方法的提案。

worst case complexity will be n * log(n) .最坏情况的复杂度将是n * log(n) n for looping all elements in series array and log(n) for binary search. n用于循环series array中的所有元素, log(n)用于二进制搜索。

 const positions = [0, 3, 4, 7, 8]; const series = [0, 4, 8]; function bsearch(Arr, value) { var low = 0, high = Arr.length - 1, mid; while (low <= high) { mid = Math.floor((low + high) / 2); if (Arr[mid] == value) return mid; else if (Arr[mid] < value) low = mid + 1; else high = mid - 1; } return -1; } let i; for (i = 0; i < series.length; ++i) { const pos = bsearch(positions, series[i]); if (pos === -1) break; } if (i === series.length) { console.log("All element present"); } else { console.log("All elmenet not present"); }

My answer to this would be to use Javascript builtins Array.prototype.every and Array.prototype.indexOf .我对此的回答是使用 Javascript 内置Array.prototype.everyArray.prototype.indexOf

It would go something like this,它会像这样 go ,

// Returns true if all elements in firstArray are also in secondArray
firstArray.every(element => secondArray.indexOf(element) !== -1)

The every builtin loops through the array, running your callback on each element and returns true if your callback returned true (or truthy values) for all elements, otherwise returns false. every内置函数遍历数组,对每个元素运行回调,如果回调对所有元素返回 true(或真值),则返回 true,否则返回 false。

The indexOf builtin searches for the argument you give it in the array with reference equality, returns the index of the argument if it is in the array, or returns -1 if it isn't. indexOf内置函数在数组中搜索具有引用相等性的参数,如果它在数组中,则返回参数的索引,否则返回 -1。

So for every element in firstArray we check if it is in secondArray and then return true if it is by checking if indexOf returns -1 or not.因此,对于 firstArray 中的每个元素,我们检查它是否在 secondArray 中,然后通过检查 indexOf 是否返回 -1 来返回 true。

Combining these, we can solve this problem in a one-liner.结合这些,我们可以一次性解决这个问题。 For unsorted arrays, this is the best you can get.对于未排序的 arrays,这是你能得到的最好的。 However, if you assume the array is sorted, you can get much faster using binary search.但是,如果您假设数组已排序,则使用二进制搜索可以更快。 As the answer before me uses.正如我之前使用的答案。

If your arrays (both of them) are sorted and they don't contain repeats, you can walk through them this way:如果您的 arrays (两者)都已排序并且它们不包含重复,则可以这样遍历它们:

 //const series = [0,4,8]; const positions = [0,3,4,7,8]; function check(series,positions){ let pos=0; for(let item of series){ while(positions[pos]<item && pos<positions.length) pos++; if(pos==positions.length || positions[pos];==item) return false; } return true. } console?log("true,",check([0,4,8];positions)). console?log("true,",check([0];positions)). console?log("false,",check([5];positions)). console?log("true,",check([8];positions)). console?log("false,",check([0,4,5];positions));

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

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