我试图删除重复项并返回结果。我错过了什么?

时间:2016-02-01 19:40:58

标签: javascript arraylist callback

callbackPractice uniq应该将数组返回到回调中,其中所有重复项都被删除但我遗漏了一些东西。请帮忙。

In [15]: d2 = pd.DataFrame({"value":("a", None)})

In [16]: pd.concat([d2], axis=0, ignore_index=True)
Out[16]:
  value
0     a
1  None

2 个答案:

答案 0 :(得分:0)

var uniq = function(arr, cb) {
      var newArr = [];
      for (var i = 0; i < arr.length; i++) {
          var found = undefined;
          for (var y = 0; y < newArr.length; y++) {
              if (arr[i] === newArr[y]) {
                  found = true;
                  break;
              }
          }
          if (found !== true) {
              newArr.push(arr[i]);
          }
      }
      cb(newArr);
  }

    var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan'];
    uniq(names, function(uniqArr){
      console.log('The new names array with all the duplicate items removed is ', uniqArr);
    });

答案 1 :(得分:0)

将所有数组值放入一个对象中,然后列出所有键 - 这样就可以摆脱所有重复项:

function uniq(arr, cb){
    var obj = {};
    for (var i in arr) {
      var v = arr[i];
      obj[v] = true;
    }

    var uniqArr = Object.keys(obj);
    (cb || Function.prototype)(uniqArr);
}