Ad

Javascript IndexOf Array Different Result On Browser

Here is my snippet

var arrays = [
  ['name1', -10, 100, 1, 'abc', 'xxx'],
  ['name1', -10, 100, 1, 'abc', 'xxx'],
  ['name2', -8, 80, 1, 'bbb', 'zzz'],
  ['name1', -10, 100, 1, 'abc', 'xxx']
];

$('#searchsite').keyup(function() {
  var keyword = $(this).val();
  var minlength = 3;
  var result = [];
  if (keyword.length >= minlength) {
    for (var i = 0; i < arrays.length; i++) {
      if (arrays[i][0].toLowerCase().indexOf(keyword) !== -1) {
        result.push('<input type="button" name="button" class="btn btn-danger" value="' + arrays[i][0] + '"><br>');
      }
    }
    resultunique = jQuery.unique(result);
    $('#searchresult').html(resultunique);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="searchsite" />
<div id="searchresult"></div>

any reason why getting different result on firefox and chrome

Ad

Answer

Your use of $.unique() is wrong, it works only on dom elements.

Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.

Since you are creating a new input element markup every time, there are no duplicates.

You can implement the unique logic yourself

var arrays = [
  ['name1', -10, 100, 1, 'abc', 'xxx'],
  ['name1', -10, 100, 1, 'abc', 'xxx'],
  ['name2', -8, 80, 1, 'bbb', 'zzz'],
  ['name1', -10, 100, 1, 'abc', 'xxx']
];

$('#searchsite').keyup(function() {
  var keyword = $(this).val().toLowerCase();
  var minlength = 3;
  var result = [],
    keys = {};
  if (keyword.length >= minlength) {

    result = $.map(arrays, function(array) {
      var name = array[0].toLowerCase();
      if (!keys[name]) {
        keys[name] = true;

        if (name.indexOf(keyword) !== -1) {
          return '<input type="button" name="button" class="btn btn-danger" value="' + array[0] + '"><br>';
        }
      }

    })

    $('#searchresult').html(result.join(''));
  } else {
    $('#searchresult').html('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="searchsite" />
<div id="searchresult"></div>

Ad
source: stackoverflow.com
Ad