javascript - How to add value to an object inside function? -
i loop through array , depending on conditions want add different values object.
the first console.log() outputs value. second 1 doesn't output anything. why? , can it? desired outcome if of keywords inside nicecontact.fulladress, string should splitted , added using keyword. if none of values are, want fulladress=adress
var nicecontact= {} nicecontact.fulladress = $.trim(contact[2]) //cut out lgh if it's in there. var keywords = ["lgh", "lgh"] nicecontact.adress = keywords.some(function(keyword){ if (nicecontact.fulladress.indexof(keyword) != -1){ adressarray = nicecontact.fulladress.split(keyword) nicecontact.adress = adressarray[0] console.log(nicecontact.adress) return adress; }else{ console.log('false') nicecontact.adress = nicecontact.fulladress } }) console.log(nicecontact.adress)
thats not array.some
for. shouldnt returning value it:
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/some
some executes callback function once each element present in array until finds 1 callback returns true value. if such element found, returns true. otherwise, returns false. callback invoked indexes of array have assigned values; not invoked indexes have been deleted or have never been assigned values.
var nicecontact= {} var hadhit = false nicecontact.fulladress = $.trim(contact[2]) //cut out lgh if it's in there. var keywords = ["lgh", "lgh"] nicecontact.adress = nicecontact.fulladress keywords.foreach(function(keyword){ if (!hadhit && nicecontact.adress.indexof(keyword) != -1){ // checking keywords here // , modify nicecontact.adress if needed // if you're looking `break` out of .foreach // can `return false;` instead (same break) see http://stackoverflow.com/questions/6260756/how-to-stop-javascript-foreach // or if have hit, `hadhit = true` after mod-ing address } }) console.log(nicecontact.adress)
Comments
Post a Comment