0

In below code I copied item to dummyItem and then changed the description property of dummyItem then why Item's description property is also getting changed. customFilter function gets called for no of items one after another.

Item structure

item = {brand: "test_brand", categorygroup: "Food & Drink", categoryid: 131, categorytext: "Breweries", description:"<b><i>the new description</i><b>", listprice: 123, noofrewards: 14, supplieditems: 1, title: "test_dev"}

customFilter : function(item, args) {
                    if (args.searchString != "" && item.productid.toFixed(2).indexOf(args.searchString.toLowerCase()) == -1 
                                                && item.shortid.toLowerCase().indexOf(args.searchString.toLowerCase()) == -1 
                                                && item.title.toLowerCase().indexOf(args.searchString.toLowerCase()) == -1
                                                && item.brand.toLowerCase().indexOf(args.searchString.toLowerCase()) == -1 
                                                && item.description.toLowerCase().indexOf(args.searchString.toLowerCase()) == -1 
                                                && item.categorytext.toLowerCase(2).indexOf(args.searchString) == -1 
                                                && item.companyid.toFixed(2).indexOf(args.searchString) == -1 
                                                && item.listprice.toFixed(2).indexOf(args.searchString) == -1 
                                                && item.currency.toLowerCase().indexOf(args.searchString.toLowerCase()) == -1) {
                        return false;
                    }
                    var dummyItem = item;
                    dummyItem.description = $('<div></div>').html(item.description).text();
                    $self.filteredData.push(dummyItem);
                    return true;
                },
vinayakj
  • 5,591
  • 3
  • 28
  • 48

1 Answers1

0

You are referring to the same object:

var dummyItem = item;

var src = {
  'a': 45
};
console.log('src, ', src);
var trg = src; // trg is pointing to src
console.log('trg, ', trg);
var cpy = {
  'a': 45
};
console.log('cpy, ', cpy);
console.log('src === trg, ', src === trg); //true
console.log('src === cpy, ', src === cpy); //false
Open console ...