6

是否有一个 Underscore.js 函数可以根据另一个对象的属性将一个对象映射到另一个对象?

类似于 AutoMapper在 .NET 中的工作方式。)

例如:

var objectA = { 'name': 'Jonathan', 'city': 'Sydney' };
var objectB = { 'name': 'Jonathan Conway', 'city': 'Sydney', 'errors': [] }

_.mapperMethod(objectB);

=> { 'name': 'Jonathan Conway', 'city': 'Sydney' };
4

3 回答 3

11

可能_.extend()

_.extend(objectA, objectB);

console.log(objectA);
// { 'name': 'Jonathan Conway', 'city': 'Sydney', 'errors': [] }

如果您不想获取其他密钥,可以将其与_.keys()and一起使用_.pick()

var keys = _.keys(objectA);
_.extend(objectA, _.pick(objectB, keys));

console.log(objectA);
// { 'name': 'Jonathan Conway', 'city': 'Sydney' }
于 2013-08-09T06:23:25.370 回答
2
Below is my auto mapper

    var sourceObj, desObj;
     var map: function (source, destination) {
                    var desKeys = _.keys(destination), functions;
                    _.extend(destination, _.pick(source, desKeys));
                    sourceObj = source;
                    desObj = destination;

                    functions = {
                        forMember: function (sourceKey, desKey) {
                            var keys = sourceKey.split('.'), sourceValue = sourceObj, index = 0;

                            // incase sourceKey is a nested object like objectA.Value
                            if (keys.length) {
                                while (index < keys.length) {
                                    sourceValue = sourceValue[keys[index]];
                                    index++;
                                }
                                desObj[desKey] = sourceValue;
                            }
                            else {
                                desObj[desKey] = sourceObj[sourceKey];
                            }

                            return functions;
                        }
                    };
                    return functions;
                }

var mapList: function (listSource, listDestination) {
                    _.each(listDestination, function(destination, i){
                        var source = listSource[i];
                         map(source,destination);
                      });

                    functions = {
                        forMember: function (sourceKey, desKey) {
                           _.each(listDestination, function(destination, i){
                                var source = listSource[i];
                                map(source,destination)
                                 .forMember(sourceKey, desKey);
                           });

                            return functions;
                        }
                    };
                    return functions;
                }


and how to use it

    var source = {
     Name: 'Nguyen Tran',
     Age: '30',
     Address: {
                 Street: '121 Le Van Viet',
                 City: 'HCM'
              }
    };

    var destination = {
         Name: 'test',
         age: '25',
         Street: '',
         City: ''
    };
        autoMapper.map(source, destination)
                  .forMember('Age', 'age')
                  .forMember('Address.Street', 'Street')
                  .forMember('Address.City', 'City')

Hope this work for you.
于 2013-12-17T06:36:06.803 回答
0

在过去的几个月里,我设法为 TypeScript / JavaScript 创建了一个非常完整的 AutoMapper 库端口:AutoMapperTS。该端口确实 - 在许多其他功能中 - 支持展平/嵌套和异步映射。

有关 AutoMapperTS 库的更多信息,包括如何使用 NPM 和 Bower 安装它,请查看 GitHub 上的库:http: //b.ldmn.nl/AutoMapperTS

于 2015-12-16T00:20:28.823 回答