如何在javascript或Jquery中删除重复数组

时间:2015-10-09 07:14:46

标签: javascript jquery arrays unique

数组

[ li.size30x30, li.size60x30, li.size60x30, li.size30x30, li.size60x30,
  li.size60x30, li.size60x30, li.size60x30, li.size30x30, 704 more… ]

期望结果数组[ li.size30x30, li.size60x30 ]

1 个答案:

答案 0 :(得分:1)

您可以在此使用 filter() indexOf()



var arrayEl = ['li.size30x30', 'li.size60x30', 'li.size60x30', 'li.size30x30', 'li.size60x30',
  'li.size60x30', 'li.size60x30', 'li.size60x30', 'li.size30x30'
];

arrayEl = arrayEl.filter(function(v, i, arr) {
  return i == arr.indexOf(v);
  // checking array element already occured before the element using indexOf(), which returns index of first occurance 
});

document.write(JSON.stringify(arrayEl));