【发布时间】:2016-10-16 03:12:55
【问题描述】:
我需要检查一个 JavaScript 数组,看是否有重复值。最简单的方法是什么?如果不需要进入json数组,我只需要检查值是否已经存在。
function cek() {
resi_or_code = document.getElementById('code_or_resi').value;
resi = resi_or_code.split(',');
if($.trim(resi_or_code) != ''){
location.href = base_url + 'resi/' + encodeURIComponent(resi_or_code);
}
if (localStorage.daftar_data){
daftar_data = JSON.parse(localStorage.getItem('daftar_data'));
$("#riwayat").toggle();
}
else {
daftar_data = [];
}
for (y in daftar_data){
var q = daftar_data[y].resis;
for (x in resi){
console.log(q);
if (q === resi[x])
{
console.log('Value exist');
}else{
console.log('Value does not exist');
daftar_data.push({'resis':resi[x]});
localStorage.setItem('daftar_data', JSON.stringify(daftar_data));
}
}
}
}
【问题讨论】:
-
两个不同问题的相同代码? stackoverflow.com/questions/37828082/…
标签: javascript arrays json duplicates local-storage