-2
"Rows":[
    [0:"2017-01-01", 1:"Ontdekkingstocht"],
    [0:"2017-01-01", 1:"Ontdekkingstocht"],
    [0:"2017-01-02", 1:"Ontdekkingstocht"]]

How do I check if 0:"2017-01-01" appears more than once, and if so, return true?

Bo Vandersteene
  • 601
  • 7
  • 16
Preamas
  • 1
  • 7

3 Answers3

1

Here's a fast way:

var data = {
  "Rows": [
    ["2017-01-01", "Ontdekkingstocht"],
    ["2017-01-01", "Ontdekkingstocht"],
    ["2017-01-02", "Ontdekkingstocht"]
  ]
};

function dupes(data) {
  var cnt = {};
  data.Rows.forEach((i) => { cnt[i[0]] = 1 });
  return Object.keys(cnt).length < data.Rows.length;
}

console.log(dupes(data));
0

Here a fonction doing that, with your array as parameter:

function checkDuplicate(arr){
    var i = 0;
    while(i < arr.length-1){
        arr.slice(i+1).forEach(function(element) {
            if(element[0] == arr[i][0])return true;
        });
    }
    return false;
}
L. Carbonne
  • 471
  • 5
  • 10
0

A fast readable way is:

const rows = [
    ["2017-01-01", "Ontdekkingstocht"],
    ["2017-01-01", "Ontdekkingstocht"],
    ["2017-01-02", "Ontdekkingstocht"]
  ];

const duplicates = rows
  .reduce((prev, curr) => [...prev, ...curr])
  .some((element, index, array) => array.filter(el => element === el));

console.log(duplicates);
Bo Vandersteene
  • 601
  • 7
  • 16