Is there any way to rename js object keys using underscore.js

后端 未结 13 1794
小蘑菇
小蘑菇 2020-12-05 01:21

I need to convert a js object to another object for passing onto a server post where the names of the keys differ for example

var a = {
    name : \"Foo\",
          


        
相关标签:
13条回答
  • 2020-12-05 02:15

    Similar to @pimvdb, you can also do it with a _.reduce:

    _.reduce(a, function(result, value, key) {
        key = map[key] || key;
        result[key] = value;
        return result;
    }, {});
    

    Fiddle: http://jsfiddle.net/T9Lnr/39/

    0 讨论(0)
  • 2020-12-05 02:16
    // key_map: {old_name1: new_name1, ... }
    function rename_keys(object, key_map, is_picked=false){
      keys = _.keys(key_map);
      new_keys = _.values(key_map);
      picked = _.pick(object, keys);
      renamed = _.object(new_keys, _.values(picked));
      if(is_picked) return renamed;
    
      return _.chain(object).omit(keys).extend(renamed).value();
    }
    

    This may be slower than above answers.

    0 讨论(0)
  • 2020-12-05 02:16

    Why don't you use this simple java script ? Value of any key:value pair should be string/number/Boolean.

    <script type="text/javascript">    
        var serverKeyMap = {
            name : "id",
            amount : "total",
            reported : "updated"
        };
    
        var a = {
            name : "Foo",
            amount: 55,
            reported : false
        };
    
        var b={}; // b is object where you will get your output
    
        for(i in serverKeyMap) b[serverKeyMap[i]]=a[i];
    
        console.log(b); // It gives what you need.
    
    </script>
    
    0 讨论(0)
  • 2020-12-05 02:16

    You can create your new custom function :

    lodash.rename = function(obj, keys, newKeys) {
      keys.map((key, index) => {
        if(lodash.includes(lodash.keys(obj), key)) {
          obj[newKeys[index]] = lodash.clone(obj[key], true);
          delete obj[key];
        }
      });
      return obj;
    };
    

    Or else if you want to edit only one keyName:

    lodash.rename = function(obj, key, newKey) {
        if(lodash.includes(lodash.keys(obj), key)) {
          obj[newKeys[index]] = lodash.clone(obj[key], true);
          delete obj[key];
        }
      return obj;
    };
    
    
    0 讨论(0)
  • 2020-12-05 02:17

    It's been solved here https://stackoverflow.com/a/30940370/1360897

    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;
        });
    });
    
    0 讨论(0)
  • 2020-12-05 02:17

    Using underscore omit and spread operator.

    a = _.omit({
      ...a,
      id: a.name,
      total: a.amount,
      updated: a.reported,
    }, ['name', 'amount', 'reported']);
    

    Key assignments below spread operator loads new keys and omit omits the old ones.

    0 讨论(0)
提交回复
热议问题