我有以下jQuery脚本:
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; $.uniqueSort(greetingsArray);有没有办法获取从阵列中删除的项目? 如果是这样,它将如何用于显示显示这些值的消息?
例如,我有以下HTML:
<div id="greetings"></div>我想使用以下jQuery脚本在该div元素中显示已删除的重复问候语:
var greetingsRemoved = ...; $("#id").html("Removed the following duplicate greetings: " + greetingsRemoved);我可以用什么来代替...来实现这一目标? 我希望这既不困难也不复杂。
I have the following jQuery script:
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; $.uniqueSort(greetingsArray);Is there a way to get the items that were removed from the array? If so, how would it be used to display a message showing these values?
For example, I have the following HTML:
<div id="greetings"></div>I want to display removed duplicate greetings in that div element with the following jQuery script:
var greetingsRemoved = ...; $("#id").html("Removed the following duplicate greetings: " + greetingsRemoved);What can I put in place of ... to make that happen? I hope this is neither too difficult nor complicated.
最满意答案
尽管使用jQuery.uniqueSort()执行此操作可能是一件坏事,因为API文档中的描述中所述的原因,出于某种原因,它的工作原理如下:
只是测试:不要使用这个例子!
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; var sortedArray = greetingsArray.slice().sort(); var greetingsDuplicate = []; for (var i = 0; i < greetingsArray.length - 1; i++) { if (sortedArray[i + 1] == sortedArray[i]) { greetingsDuplicate.push(sortedArray[i]); } } $("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", ")); $.uniqueSort(greetingsArray); $("#unique").html("Unique Greetings: " + greetingsArray.sort().join(", "));<div id="unique"></div> <div id="duplicate"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>但是,有一种更好的方法可以通过实现从此处提供的两个答案派生的方法来实现此目的 ,而不是更准确地显示您想要的结果。
实际答案
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; var greetingsUnique = []; $.each(greetingsArray, function(index, value){ if($.inArray(value, greetingsUnique) === -1) greetingsUnique.push(value); }); $("#unique").html("Unique Greetings: " + greetingsUnique.sort().join(", ")); var sortedArray = greetingsArray.slice().sort(); var greetingsDuplicate = []; for (var i = 0; i < greetingsArray.length - 1; i++) { if (sortedArray[i + 1] == sortedArray[i]) { greetingsDuplicate.push(sortedArray[i]); } } $("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", "));<div id="unique"></div> <div id="duplicate"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>Although it is probably a bad thing to use jQuery.uniqueSort() for doing this because of the reason stated in its description within the API Documentation, for some reason, it works as intended as follows:
Just testing: Do not use this example!
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; var sortedArray = greetingsArray.slice().sort(); var greetingsDuplicate = []; for (var i = 0; i < greetingsArray.length - 1; i++) { if (sortedArray[i + 1] == sortedArray[i]) { greetingsDuplicate.push(sortedArray[i]); } } $("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", ")); $.uniqueSort(greetingsArray); $("#unique").html("Unique Greetings: " + greetingsArray.sort().join(", "));<div id="unique"></div> <div id="duplicate"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>However, there is a better way to do this by implementing the methods derived from both answers provided here and here instead to more accurately display the results you desire.
The Actual Answer
var greetingsArray = [ "Hello, Debbie!", "Hello, Randy!", "Hello, Carl!" , "Hello, Patrick!", "Hello, Susan!", "Hello, Susan!", "Hello, Carl!" ]; var greetingsUnique = []; $.each(greetingsArray, function(index, value){ if($.inArray(value, greetingsUnique) === -1) greetingsUnique.push(value); }); $("#unique").html("Unique Greetings: " + greetingsUnique.sort().join(", ")); var sortedArray = greetingsArray.slice().sort(); var greetingsDuplicate = []; for (var i = 0; i < greetingsArray.length - 1; i++) { if (sortedArray[i + 1] == sortedArray[i]) { greetingsDuplicate.push(sortedArray[i]); } } $("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", "));<div id="unique"></div> <div id="duplicate"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
更多推荐
发布评论