有没有办法将jQuery.uniqueSort()中删除的项存储在变量中?(Is there a way to store the items that are removed from jQuery

编程入门 行业动态 更新时间:2024-10-28 18:28:05
没有办法将jQuery.uniqueSort()中删除的项存储在变量中?(Is there a way to store the items that are removed from jQuery.uniqueSort() in a variable?)

我有以下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> 
  
 

更多推荐

本文发布于:2023-04-28 08:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   变量   uniqueSort   jQuery   variable

发布评论

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

>www.elefans.com

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