查找在Javascript中比较2个数组元素缺失

编程入门 行业动态 更新时间:2024-10-26 16:22:57
本文介绍了查找在Javascript中比较2个数组元素缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

出于某种原因,我有包装我的脑海里解决这个问题的一些严重的困难。我需要接受2个数组,比较2,然后返回缺少元素的字符串这个JS功能。例如。发现在这是出现在previous数组中的currentArray缺少的元素。

For some reason I'm having some serious difficulty wrapping my mind around this problem. I need this JS function that accepts 2 arrays, compares the 2, and then returns a string of the missing element. E.g. Find the element that is missing in the currentArray that was there in the previous array.

function findDeselectedItem(CurrentArray, PreviousArray){ var CurrentArrSize = CurrentArray.length; var PrevousArrSize = PreviousArray.length; // Then my brain gives up on me... // I assume you have to use for-loops, but how do you compare them?? return missingElement; }

感谢提前!我不要求为code,但哪怕只是在正确的方向上推或暗示可以帮助...

Thank in advance! I'm not asking for code, but even just a push in the right direction or a hint might help...

推荐答案

这应该工作。你也应该考虑其中数组的元素实际上的情况太阵列。如预期那么的indexOf可能无法正常工作。

This should work. You should also consider the case where the elements of the arrays are actually arrays too. The indexOf might not work as expected then.

function findDeselectedItem(CurrentArray, PreviousArray) { var CurrentArrSize = CurrentArray.length; var PreviousArrSize = PreviousArray.length; // loop through previous array for(var j = 0; j < PreviousArrSize; j++) { // look for same thing in new array if (CurrentArray.indexOf(PreviousArray[j]) == -1) return PreviousArray[j]; } return null; }

更多推荐

查找在Javascript中比较2个数组元素缺失

本文发布于:2023-11-30 10:08:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   缺失   元素   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!