1

how to check the contents of the array of doubles , if there is an array of [ 1,2,2,3,1 ] so the value you 've entered will not be entered again ?

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 (x in resi){
        console.log(localStorage.daftar_data);
        daftar_data.push({'resis':resi[x]});
        localStorage.setItem('daftar_data', JSON.stringify(daftar_data));
    }
}
Andrew
  • 1,858
  • 13
  • 15
Iswadi
  • 87
  • 1
  • 1
  • 8
  • http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array – epascarello Jun 15 '16 at 04:46
  • [Object, Object, Object] 0 : Object resis : "030023706726" __proto__ : Object 1 : Object 2 : Object length : 3 __proto__ : Array[0] – Iswadi Jun 15 '16 at 05:31
  • 1
    if your are using any utils libraries like lodash look into https://lodash.com/docs#uniq use combinations _.merge and _.uniq or _.uniqBy to get a solution. i didn't get your question clearly so i'm not able to provide a complete solution which can help you. – scb Jun 15 '16 at 05:43

2 Answers2

1

You can use Array.prototype.includes():

for (x in resi){
  console.log(localStorage.daftar_data);
  if(!daftar_data.includes(resi[x])){ 
      daftar_data.push({'resis':resi[x]}); 
  }
}
localStorage.setItem('daftar_data', JSON.stringify(daftar_data));
Jai
  • 74,255
  • 12
  • 74
  • 103
  • I love ES6 but if IE support is required [array.some](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some) could be an efficient alternative until Microsoft eventually gets its act together. – ste2425 Jun 15 '16 at 06:30
  • comparing data localstrorage denagn input data – Iswadi Jun 15 '16 at 06:58
0

you can use the indexOf also i.e. Array.prototype.indexOf(el) where el is the element.indexOf method returns the index of the element in the array, if it is not present then it returns -1.

for (x in resi){
   if(daftar_data.indexOf(resi[x]) > -1) {
      daftar_data.push({'resis':resi[x]});
   }
}
localStorage.setItem('daftar_data', JSON.stringify(daftar_data));
isambitd
  • 829
  • 8
  • 14
  • 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)); } } } – Iswadi Jun 15 '16 at 06:57
  • comparing data localstrorage denagn input data – Iswadi Jun 15 '16 at 06:58
  • @Iswadi Can you please explain what you want to say. I think you put your code in wrong box. – isambitd Jun 15 '16 at 07:00