【问题标题】:Check that the array is unique [duplicate]检查数组是否唯一[重复]
【发布时间】:2012-11-05 12:26:43
【问题描述】:

可能重复:
Javascript: Determine whether an array contains a value

var thelist = new Array();
function addlist(){
thelist.push(documentgetElementById('data').innerHTML);
}

如何检查我推送的数据是否已经存在于数组thelist中?

【问题讨论】:

    标签: javascript


    【解决方案1】:
    var thelist = []; // Use the array literal, not the constructor.
    function addlist(){
    
      // get the data we want to make sure is unique
      var data = documentgetElementById('data').innerHTML;
    
      // make a flag to keep track of whether or not it exists.
      var exists = false;
    
      // Loop through the array
      for (var i = 0; i < thelist.length; i++) {
    
        // if we found the data in there already, flip the flag
        if (thelist[i] === data) {
          exists = true;
    
          // stop looping, once we have found something, no reason to loop more.
          break;
        }
      }
    
      // If the data doesn't exist yet, push it on there.
      if (!exists) {
        thelist.push(data);
      }
    }
    

    【讨论】:

    • 不妨将break; 放在if 块内,该块位于for 循环内
    • 并使用Array#indexOf,因为几乎所有引擎都有它(对那些没有的引擎使用polyfill)。希望本机实现可能比您的循环更快(这绝不是保证)。
    • @T.J.Crowder 是的,这基本上删除了整个for 循环的东西,并允许一个/两个衬里。我打算发布一个答案,但是这个答案就在这里,基本上就是这样做的。
    【解决方案2】:

    如果你不关心 IE

    var thelist = [1, 2, 3];
    
    function addlist(data) {
    
        alreadyExists = thelist.some(function (item) {
            return item === data
        });
    
        if (!alreadyExists) {
            thelist.push(data);
        }
    }
    addlist(1);
    addlist(2);
    addlist(5);
    
    console.log(thelist);​
    

    http://jsfiddle.net/C7PBf/

    Some 确定是否存在至少一个具有给定约束(回调返回值 === true)的元素。

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some

    【讨论】:

      【解决方案3】:

      如果不关心 IE 8 或更低版本,可以使用Array.filter

      var thelist = new Array();
      function addlist(){
          var val = documentgetElementById('data').innerHTML;
          var isInArray = theList.filter(function(item){
              return item != val
          }).length > 0;
      
          if (!isInArray)
              thelist.push(val);
      }
      

      或者,你可以使用Array.indexOf

      var thelist = new Array();
      function addlist(){
          var val = documentgetElementById('data').innerHTML;
          var isInArray = theList.indexOf(val) >= 0;
      
          if (!isInArray)
              thelist.push(val);
      }
      

      【讨论】:

        【解决方案4】:

        看看underscore.jsunderscore.js 然后您可以将数组检查为

        _.contains(thelist, 'value you want to check');
        
        // The full example
        var thelist = new Array();
        function addlist(){
           var data = documentgetElementById('data').innerHTML;
           if(!_.contains(thelist, data)) theList.push(data);
        }
        

        或者你可以在不考虑重复值的情况下将值添加到数组中,并且在添加过程完成后,你可以删除重复的元素

        theList = _.uniq(theList);
        

        第二种方法当然效率较低。

        【讨论】:

        • 整个库对于像这样的简单任务来说是多余的。
        • 是的,但答案仍然是正确的,如果 OP 代码中还有其他可以通过库简化的功能,也许会有用。
        • @MarkThomas 比较简单易学,但是如果你想学习JavaScript或者喜欢Vanilla JS(像我一样)就不太好。
        猜你喜欢
        • 1970-01-01
        • 2018-04-08
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多