Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remap properties name and values using lodash

I have this 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 would like use lodash to obtain this 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 with other names and change the value for some specific properties. Can I do it using lodash?

like image 884
padibro Avatar asked Jun 19 '15 14:06

padibro


People also ask

How do I change the key name in Lodash?

renameKeys() Method. The Lodash _. renameKeys() method takes an object and a mapping object and returns a new object where the keys of the given object have been renamed as the corresponding value in the keyMap.

What is _ get?

The _. get() function is an inbuilt function in the Underscore. js library of JavaScript which is used to get the value at the path of object. If the resolved value is undefined, the defaultValue is returned in its place. Syntax: _.get(object, path, [defaultValue])

How do I compare two arrays of objects in Lodash?

In Lodash, we can deeply compare two objects using the _. isEqual() method. This method will compare both values to determine if they are equivalent.

What does Lodash map do?

Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _. map() method creates an array of values by running each element in collection through the iteratee. There are many lodash methods that are guarded to work as iteratees for methods like _.


2 Answers

Yes, since lodash v3.8.0 you can remap objects in any way desireable

ES5 code

var items = [ { oldKey: 'oldValue' /*...*/ } ]

var keyMapping = { oldKey: 'newKey' /*...*/ }
var valueMapping = { oldValue: 'newValue' /*...*/ }

var remapper = function(item){
  return _(item) // lodash chain start
    .mapKeys( function(v, k){ return keyMapping[k] } )
    .mapValues( function(v){ return valueMapping[v] } )
    .value() // lodash chain end
}

var remappedItems = items.map(remapper)

ES2015/ES6 code

let items = [ { oldKey: 'oldValue' /*...*/ } ]

let keyMapping = { oldKey: 'newKey' /*...*/ }
let valueMapping = { oldValue: 'newValue' /*...*/ }

let remapper = item => _(item) // lodash chain start
  .mapKeys( (v, k)=> keyMapping[k] )
  .mapValues( v => valueMapping[v] )
  .value() // lodash chain end

let remappedItems = items.map(remapper)
like image 158
Alexander Gonchiy Avatar answered Oct 16 '22 11:10

Alexander Gonchiy


Create a mapping of old and new keys, like this

var keyMapping = {'PropertyA': 'propertyA', ..., 'PropertyF': 'propertyNEW'}

and also a mapping of old and new values, like this

var valueMapping = {'Y': true, 'F': false}

And then using _.map and _.transform, you can transform the object, like 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;
    });
});
like image 21
thefourtheye Avatar answered Oct 16 '22 11:10

thefourtheye