javascript 使用 lodash 重新映射属性名称和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30940120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:58:18  来源:igfitidea点击:

Remap properties name and values using lodash

javascriptunderscore.jslodash

提问by padibro

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 lodashto obtain this result:

我想使用lodash来获得这个结果:

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?

我想用其他名称映射每个属性名称并更改某些特定属性的值。我可以使用lodash吗?

采纳答案by thefourtheye

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 _.mapand _.transform, you can transform the object, like this

然后使用_.mapand _.transform,你可以转换对象,像这样

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;
    });
});

回答by Alexander Gonchiy

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

是的,从 lodash v3.8.0 开始,您可以以任何需要的方式重新映射对象

ES5 code

ES5代码

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

ES2015/ES6 代码

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)