我需要檢查一個JavaScript數組,看看是否有重複的值。什麼是最簡單的方法來做到這一點?我只需要檢查值是否已經存在,如果不需要進入json數組。json數組檢查是否不需要在localstorage中插入?
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));
}
}
}
}
兩個不同的問題相同的代碼? http://stackoverflow.com/questions/37828082/remove-duplicate-input-value-using-javascript#37828082 – brk