javascript - Using indexOf( ) to check if particular object exists in JS Array -
this structure of array:
[{"stockcode":"pallet cards","quantity":"2"}, {"stockcode":"pallet cards","quantity":"3"}, {"stockcode":"cbl202659/a","quantity":"1"}, {"stockcode":"cbl201764","quantity":"3"}]
when order button
clicked check if stockcode
exists in array
. each time -1 returned.
this code check stockcode
:
$(".orderbtn").click(function(event){ //check ensure quantity > 0 if(quantity == 0){ console.log("quantity must greater 0") }else{//it continue //show order box $(".order-alert").show(); event.preventdefault(); //get reference product clicked var stockcode = $(this).closest('li').find('.stock_code').html(); //get reference quantity selected var quantity = $(this).closest('li').find('.order_amount').val(); //order item (contains stockcode , quantity) - can add whatever data here var orderitem = { 'stockcode' : stockcode, 'quantity' : quantity }; //check if cookie exists if($.cookie('order_cookie') === undefined){ console.log("creating new cookie"); //add object array productarray.push(orderitem); }else{//already exists console.log("updating cookie") productarray = json.parse($.cookie('order_cookie')); //check if item exists in cookie , update qty if(productarray.indexof(stockcode)!= -1){ //get original item , update console.log("updating existing entry " + productarray.indexof("pallet cards")); } else{ console.log("adding new entry "); //insert item array //productarray.push(orderitem); } } } //update cookie $.cookie('order_cookie', json.stringify(productarray), { expires: 1, path: '/' }); //testing output of cookie console.log($.cookie('order_cookie')); });
i reference stockcode when user clicks on order button
:
var stockcode = $(this).closest('li').find('.stock_code').html();
i check if stockcode in array
, if not adding new entry, rather, updating existing one.
i hope i'm getting right:
function findbystockcode(code, stockcodearr){ return stockcodearr.filter(function(elem){ return elem.stockcode == code; }); }
this ofcourse give array back. if length greater 0 element given code in array already. think filter-function has been added @ ecma5 ie8 won't support it.
[https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/filter][1] mentions fallback case filter not implemented in current browser.
anyway there's similiar jquery-function in 'this' refers actual element:
function jqueryfindbystockcode(code, stockcodearr){ return $(stockcodearr).filter(function(){ return this.stockcode == code; }); }
edit:
dhruvpathak mentioned $.grep might more appropriate solution jquery's filter. (grep vs filter in jquery?)
looked again solution better performance (seems) have write on own (it's pretty simple anyway):
//for defined, non-null values function findfirstbyproperty(arr, prop, value){ for(var = 0 ; < arr.length; i++){ if(arr[i]!=null && "undefined" !== typeof arr[i] && arr[i][prop] == value) return arr[i]; } return null; }
this should have better performance (especially big arrays). in average (assuming there's such element in array) should in average twice fast filter , grep (since stops @ first match).
Comments
Post a Comment