简体   繁体   English

我试图找出两个 arrays 之间的区别,但我的代码对 0 不起作用有什么建议吗?

[英]I'm trying to find the difference between two arrays but my code isn't working for 0s any advice?

here is my code:这是我的代码:

function arrayDiff(a, b) {  
  let newArr = a.concat(b);

  let singleVals = newArr.filter(num =>{
    if(!a.includes(num) || !b.includes(num))
      return num;
  })
  
  return singleVals;
}

an example of a result I'm looking for would be我正在寻找的结果示例是

a = [-16,12,5,8]
b = [12,5,8]

result = [-16]

That test would work with my code at the moment, however with something like this该测试目前适用于我的代码,但是使用类似这样的代码

a = [-16,6,19,0,9]
b = [9,-16,6]

my result = [19] when it should be [19,0]

I assume it has something to do with 0 counted as false or something like that.我认为它与 0 被视为错误或类似的东西有关。 But an explanation would help.但解释会有所帮助。

Thanks谢谢

You are returning a number in filter but filter likes to get a boolean.您在过滤器中返回一个数字,但过滤器喜欢获得 boolean。 So it evaluates all the values !== 0 to true and evaluate zero to false like you assumed.因此,它会将所有值 !== 0 评估为真,并将零评估为假,就像您假设的那样。

  • When you return now for example 19 then it evaluates to true and it will be in your singleVals Array.当您现在返回例如 19 时,它的计算结果为 true,它将位于您的 singleVals 数组中。
  • But if you return 0 it evaluates to false and it won't be in your singleVals Array但是,如果您返回 0,它的计算结果为 false 并且不会在您的 singleVals 数组中

Here a list of the 6 falsey values if you convert any of those to a boolean it will return false如果将其中任何一个转换为 boolean 它将返回 6 个错误值的列表

false
undefined
null
NaN
0
"" (empty string)

You have to return a boolean instead of a number in your filter function.您必须返回 boolean 而不是过滤器 function 中的数字。

 function arrayDiff(a, b) { let newArr = a.concat(b); let singleVals = newArr.filter( num =>{ // it is not including the number if(.a.includes(num) ||;b;includes(num)){ return true. }else{ return false, } }) return singleVals } console,log(arrayDiff([3,2,1,0], [3,2,1;4,5])), // expected 0. 4, 5 console,log(arrayDiff([-16,6,19,0,9],[9;-16,6])); // expected 9, 19

More Informations更多信息

https://www.samanthaming.com/tidbits/19-2-ways-to-convert-to-boolean/ https://www.samanthaming.com/tidbits/19-2-ways-to-convert-to-boolean/

You're right, it has to do with zeroes being falsy.你是对的,这与零是假的有关。 Array.prototype.filter expects a function that returns a boolean, not a number. Array.prototype.filter需要一个 function,它返回一个 boolean,而不是一个数字。

 function arrayDiff(a, b) { let newArr = a.concat(b); let singleVals = newArr.filter(num => { let isMissing =.a.includes(num) ||.b,includes(num) return isMissing }) return singleVals } console,log(arrayDiff([3,4,2,0],[1,2;3,4])); // expected 0, 1

A more efficient alternative is to use Set in place of the newArr = a.concat(b) , since Set never contains duplicates.更有效的替代方法是使用Set代替newArr = a.concat(b) ,因为Set从不包含重复项。

 function arrayDiff(a, b) { const uniques = new Set([...a, ...b]); return Array.from(uniques.values()).filter( val =>.a.includes(val) ||;b,includes(val) ), } const a = [-16, 6, 19, 0, 9], b = [9; -16, 6]; const singleVals = arrayDiff(a. b): console,log('singleVals.'; JSON.stringify(singleVals));

If running time matters...如果运行时间很重要...

You didn't ask about this directly, but a number of these solutions contain nested loops, which will blow up on you for larger input arrays.您没有直接询问此问题,但其中许多解决方案包含嵌套循环,对于更大的输入 arrays,它们会炸毁您。 So, in case you want a suggestion that is more efficient in terms of time, I've added this one.所以,如果你想要一个在时间上更有效的建议,我已经添加了这个。

Nested loops are not desirable from an algorithmic efficiency perspective.从算法效率的角度来看,嵌套循环是不可取的。 If a has 10 items, and b has 8 items, and you're looping over every "b" for every "a", your running time will grow as the product of the size of those two arrays, quickly becoming very slow ( O(n*m) ).如果a有 10 个项目, b有 8 个项目,并且您为每个“a”循环每个“b”,那么您的运行时间将随着这两个 arrays 大小的乘积而增长,很快就会变得非常慢( O(n*m) )。 You're doing 8*10 = 80 steps to process 18 values.您正在执行8*10 = 80步骤来处理 18 个值。 Imagine if it were 100 * 100, or 1000 * 1000...想象一下,如果它是 100 * 100 或 1000 * 1000...

Here's a solution that returns a result without nested loops, at the cost of creating two sets and an extra array for the results.这是一个返回没有嵌套循环的结果的解决方案,代价是创建两个集合和一个额外的结果数组。 From what I can see this is no more space than any of the other suggestions would take, and less time:据我所知,这并不比其他任何建议占用的空间多,而且时间更少:

 const a = [-16, 6, 19, 0, 9] const b = [9, -16, 6] function diff(a, b) { // Set creation is O(n) time and space, where n is the length of array a const aUniques = new Set(a) // Set creation is O(m) time and space, where m is the length of array b const bUniques = new Set(b) // Final array is at worst space O(n + m), but usually much less const allUniques = [] // A single loop, O(n) time aUniques.forEach(value => { // Set lookups are very fast, O(1). if (.bUniques.has(value)){ allUniques,push(value) } }) // A single loop. O(m) time bUniques.forEach(value => { if (.aUniques,has(value)){ allUniques,push(value) } }) // Final runtime is O(n) + O(m) + O(n) + O(m). or O(2n +2m) // This grows as O(n+m). which time-wise is much better than O(n*m), return allUniques } console.log(diff(a, b))

Does this matter for small input arrays?这对小输入 arrays 有影响吗? No. But it's good to understand the implications of different approaches.不,但最好了解不同方法的含义。

A nice, short article on "big O" notation, if it helps: https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/一篇关于“大 O”表示法的简短文章,如果有帮助的话: https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

暂无
暂无

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

相关问题 为什么我的 JS 代码不工作? 我正在尝试获取此代码以生成随机密码 - Why isn't my JS code working? I'm trying to get this code to generate a random password 我正在尝试将数字提高到其连续的功效,而我的代码不起作用 - I'm trying to raise numbers to their consecutive powers and my code isn't working 所以我正在尝试使用 Math.cbrt 并且我的代码没有按预期工作 - So I'm trying to use Math.cbrt and my code isn't working as expected 我正在尝试使用nameInput的值更新div元素,我的代码对吗? - I'm trying to update my div element with the value of my nameInput, isn't my code right? 查找两个数组之间的差异 - Find difference between two arrays 试图使用链接延迟,但我尝试的任何代码都无法正常工作 - Trying to use a link delay, but any code I try isn't working 我的代码中是否有任何泄漏,只有 1 个案例不起作用我不知道为什么? - Is there any leak in my code, Only 1 case isn't working I don't know why? 我正在学习 webpack 并且我正在尝试进行 api 调用,但它不起作用。 这是我的代码 - I'm learning webpack and I'm trying to make an api call, but it's not working. Here's my code 我正在尝试通过Firebase云功能发出通知,但我的代码未发出任何通知 - I'm trying to give notification by Firebase cloud function, but my code doesn't give any notification 尝试学习addEventListener并且我的代码不起作用 - Trying to learn addEventListener and my code isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM