存在哪些 JavaScript 对象到对象映射库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10684695/
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
What JavaScript object to object mapping libraries exist?
提问by thomaux
I'm currently working on a big JavaScript project and I'm struggling with mapping incomming JSON data (from the backend) to my own JavaScript objects.
我目前正在处理一个大型 JavaScript 项目,并且正在努力将传入的 JSON 数据(从后端)映射到我自己的 JavaScript 对象。
I am using the Knockout JavaScript MVVM framework and although it includes a mapping plugin, it does not allow me to actually remap properties. I want to achieve this because the incomming JSON data is too fine grained, and I would like to 'flatten' my JS objects. An example follows.
我正在使用 Knockout JavaScript MVVM 框架,虽然它包含一个映射插件,但它实际上不允许我重新映射属性。我想实现这一点,因为传入的 JSON 数据粒度太细,我想“展平”我的 JS 对象。下面是一个例子。
Incomming data.
传入数据。
Object : {
Description: {
Id : 1,
Title : 'ProductX'
},
Price : {
Last : 12,
Currency : 3
}
}
And I would like to remap/flatten this to:
我想将其重新映射/展平为:
var mappedObject = {
id : 1,
title: 'ProductX',
price : 12,
currency : 3
}
Hence I would like to provide a mapping configuration, detailing what incomming properties should be mapped to what outgoing ones. Much like Dozeris being configured.
因此,我想提供一个映射配置,详细说明应将哪些传入属性映射到哪些传出属性。就像正在配置Dozer一样。
My question is: are there any libraries out there capable of what I'd like to achieve, or will this require me to build my own library?
我的问题是:是否有任何库能够实现我想要实现的目标,或者这是否需要我构建自己的库?
采纳答案by Samir Hafez
Actually, the knockoutjs mapping plugin allows you to do just that:
实际上,knockoutjs 映射插件允许您这样做:
In the ko.mapping.fromJS
call you can provide a mapping object that will be used to map the containing properties...
在ko.mapping.fromJS
调用中,您可以提供一个映射对象,该对象将用于映射包含的属性...
var mapper = {
create: function(options){
return { name: ko.observable(options.data.name) };
}
};
This means that using this mapper with the mapping plugin, every object will be flattened to an object containing only it's name as an observable.
这意味着将此映射器与映射插件一起使用,每个对象都将被展平为一个仅包含其名称作为可观察对象的对象。
You use it like this:
你像这样使用它:
var viewModel = ko.mapping.fromJS({id: 1, name: "a", desc: "b"}, mapper);
In this case, viewModel
will only have a property name.
在这种情况下,viewModel
将只有一个属性名称。
You can read more about this feature in the official documentation here.
您可以在此处的官方文档中阅读有关此功能的更多信息。
回答by Florian Margaine
Well, I don't think there is any library for this as this sounds quite easy to do.
好吧,我不认为有任何图书馆可以做到这一点,因为这听起来很容易。
Here is an example:
下面是一个例子:
var obj = {
Description: {
Id : 1,
Title : 'ProductX'
},
Price : {
Last : 12,
Currency : 3
}
},
mappedObject = {};
function mapThat( obj, mappedObject ) {
Object.keys( obj ).forEach( function( key ) {
if ( typeof obj[ key ] === 'object' ) {
// If it's an object, let's go recursive
mapThat( obj[ key ], mappedObject );
}
else {
// If it's not, add a key/value
mappedObject[ key.toLowerCase() ] = obj[ key ];
}
} );
}
mapThat( obj, mappedObject );
console.log( mappedObject );?
Demo here: http://jsfiddle.net/qG6hm/
演示在这里:http: //jsfiddle.net/qG6hm/
回答by DotBert
It has been a while since this topic was last updated. Since people are probably still searching for object-to-object mappers nowadays:
距离上次更新这个话题已经有一段时间了。由于人们现在可能仍在寻找对象到对象的映射器:
Last year, I have created a port of the C# AutoMapper implementation to TypeScript / JavaScript exactly for this scenario. I have put the code at GitHub (https://b.ldmn.nl/AutoMapperTS). You can also use the library directly using the automapper-ts NPM or Bower package.
去年,我为这个场景创建了一个 C# AutoMapper 实现到 TypeScript/JavaScript 的端口。我已将代码放在 GitHub ( https://b.ldmn.nl/AutoMapperTS) 上。您还可以使用 automapper-ts NPM 或 Bower 包直接使用该库。
The library is almost fully documented. Furthermore, quite a lot of Jasmine unit tests are already available (code coverage is about 95%). They should provide you with some explanation of what you need.
该库几乎有完整的文档记录。此外,已有相当多的 Jasmine 单元测试可用(代码覆盖率约为 95%)。他们应该向您解释您需要什么。
I hope this library suits your needs. Should you have any questions and/or remarks, please don't hesitate contacting me!
我希望这个库适合您的需求。如果您有任何问题和/或意见,请不要犹豫与我联系!
回答by krishna kanth
- you can use Object-mapper, this is very basic and simple library for object mapping in Javascript/JSON. when using this library, you need to specify the mapping property combinations and source and target types in a separate file.
- 你可以使用Object-mapper,这是一个非常基本和简单的 Javascript/JSON 对象映射库。使用此库时,您需要在单独的文件中指定映射属性组合以及源和目标类型。
- Also there is another library AutoMapper, this is slightly advanced. It can take care of mappings automatically based on property names, But you can still mention custom mapping for which ever it's not straight forward. If you're from .net background, this library implements in the same way as C# automapper.
- 还有另一个库AutoMapper,这有点高级。它可以根据属性名称自动处理映射,但是您仍然可以提及自定义映射,因为它不是直截了当的。如果您来自 .net 背景,则该库的实现方式与 C# automapper 相同。