javascript - Remap properties name and values using lodash -
i have array:
aitems = [{ "propertya": "apple", "propertyb": "banana", "propertyc": "dog", "propertyd": "hotdog", "propertye": "coldcat", "propertyf": "y", "propertyg": "n" }, ..., { "propertya": "this", "propertyb": "is", "propertyc": "json", "propertyd": "code", "propertye": "wow", "propertyf": "n", "propertyg": "n" }]
i use lodash obtain result:
aitems = [{ "propertya": "apple", "propertyb": "banana", "propertyc": "dog", "propertyd": "hotdog", "propertye": "coldcat", "propertynew": true, "propertyg": false }, ..., { "propertya": "this", "propertyb": "is", "propertyc": "json", "propertyd": "code", "propertye": "wow", "propertynew": false, "propertyg": false }]
i want map each property name other names , change value specific properties. can using lodash?
create mapping of old , new keys, this
var keymapping = {'propertya': 'propertya', ..., 'propertyf': 'propertynew'}
and mapping of old , new values, this
var valuemapping = {'y': true, 'f': false}
and using _.map
, _.transform
, can transform object, this
var result = _.map(allitems, function(currentobject) { return _.transform(currentobject, function(result, value, key) { if (key === 'propertyf' || key === 'propertyg') { value = valuemapping(value); } result[keymapping[key]] = value; }); });
Comments
Post a Comment