How to use contents of one array to set contents of other array to true in javascript? -
i've got 2 javascript arrays. 1 default array:
propertytypes = [ {key:'office', value:false}, {key:'residential', value:false}, {key:'retail', value:false}, {key:'industrial', value:false}, {key:'hotel', value:false} ];
and array looks this:
property_types: [ { "type": "office" }, { "type": "retail" } ]
i want set values in first array true
occur in second array type first array this:
propertytypes = [ {key:'office', value:true}, // 1 should true {key:'residential', value:false}, {key:'retail', value:true}, // , 1 {key:'industrial', value:false}, {key:'hotel', value:false} ];
i had ideas using kind of double nested loop, none of them seem make sense.
does know how can in logical way? tips welcome!
you can use
arr.map
achieve this. map() method creates new array results of calling provided function on every element in array.
try this:
var propertytypes = [{ key: 'office', value: false }, { key: 'residential', value: false }, { key: 'retail', value: false }, { key: 'industrial', value: false }, { key: 'hotel', value: false }]; var property_types = [{ "type": "office" }, { "type": "retail" }]; propertytypes.map(function(item) { (var = 0, ilen = property_types.length; < ilen; i++) { if (property_types[i].type == item.key) { item.value = true; break; } } }); alert(json.stringify(propertytypes));
Comments
Post a Comment