Underscore.js对象 - 对象映射器?

时间:2013-08-09 06:04:56

标签: javascript node.js underscore.js

是否有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' };

3 个答案:

答案 0 :(得分:10)

可能是_.extend()

_.extend(objectA, objectB);

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

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

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

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

答案 1 :(得分: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.

答案 2 :(得分:0)

在过去的几个月里,我成功地为TypeScript / JavaScript创建了一个非常完整的AutoMapper库端口:AutoMapperTS。该端口(包括许多其他功能)支持展平/嵌套和异步映射。

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