javascript - How to change property content in array? -
i have array:
var arr=[{id:'3',status:true, objectid:'23'}, {id:'4',status:false, objectid:'5'}, {id:'5',status:true, objectid:'78'}, {id:'6',status:false, objectid:'54'}, {id:'7',status:true, objectid:'85'}]
the status boolean
type.
in arr variable need change content of properties.
where status
true
need set fixed.
where status
false
need set damaged.
here desired result:
var arr=[{id:'3',status:fixed, objectid:'23'}, {id:'4',status:damaged, objectid:'5'}, {id:'5',status:fixed, objectid:'78'}, {id:'6',status:damaged, objectid:'54'}, {id:'7',status:fixed, objectid:'85'}]
what best way implement it?
try approach
use angular.foreach have mentioned in tags
var arr=[{id:'3',status:true, objectid:'23'}, {id:'4',status:false, objectid:'5'}, {id:'5',status:true, objectid:'78'}, {id:'6',status:false, objectid:'54'}, {id:'7',status:true, objectid:'85'}] angular.foreach(arr, function(value, key) { value.status = value.status ? 'fixed' : 'damaged'; }); console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
or without angular
can do
var arr=[{id:'3',status:true, objectid:'23'}, {id:'4',status:false, objectid:'5'}, {id:'5',status:true, objectid:'78'}, {id:'6',status:false, objectid:'54'}, {id:'7',status:true, objectid:'85'}] arr.foreach(item => item.status = item.status ? 'fixed' : 'damaged'); console.log(arr);
as mentioned in comment, approach not work in ie 8
Comments
Post a Comment