What JavaScript objects exist for object mapping libraries?

I am currently working on a large JavaScript project, and I am struggling with mapping JSON input (from the backend) to my own JavaScript objects.

I use the MVVM JavaScript framework for knockout and although it does include a mapping plugin , it does not allow me to reassign properties. I want to achieve this because the JSON input is too small, and I would like to “smooth out” my JS objects. The following is an example.

Incoming data.

Object : { Description: { Id : 1, Title : 'ProductX' }, Price : { Last : 12, Currency : 3 } } 

And I would like to reassign / hide this:

 var mappedObject = { id : 1, title: 'ProductX', price : 12, currency : 3 } 

Therefore, I would like to provide a mapping configuration that details which inclusion attributes should be mapped to outbound . Very similar to Dozer .

My question is: are there libraries that are capable of what I would like to achieve , or will this require me to create my own library?

+8
javascript mapping
source share
3 answers

In fact, the mapping knockoutjs plugin allows you to do just that:

In a call to ko.mapping.fromJS you can provide a mapping object that will be used to map the contained properties ...

 var mapper = { create: function(options){ return { name: ko.observable(options.data.name) }; } }; 

This means that using this mapping module with plagiarized mapping, each object will be flattened to an object containing only its name as an observable.

You use it as follows:

 var viewModel = ko.mapping.fromJS({id: 1, name: "a", desc: "b"}, mapper); 

In this case, viewModel will only have the property name.

You can read more about this feature in the official documentation here .

+3
source share

Well, I don’t think there is a library for this, as it sounds pretty easy.

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 an object, let go recursive mapThat( obj[ key ], mappedObject ); } else { // If it not, add a key/value mappedObject[ key.toLowerCase() ] = obj[ key ]; } } ); } mapThat( obj, mappedObject ); console.log( mappedObject );​ 

Demo here: http://jsfiddle.net/qG6hm/

+7
source share

Some time has passed since the last update of this topic. Since people are probably still searching for object objects now:

Last year, I created the C # AutoMapper implementation port for TypeScript / JavaScript for this particular scenario. I put the code in GitHub ( https://b.ldmn.nl/AutoMapperTS ). You can also use the library directly using Automapper-ts NPM or Bower.

The library is almost fully documented. In addition, quite a few Jasmine unit tests are already available (code coverage is about 95%). They should provide you with some explanation of what you need.

I hope this library meets your needs. If you have any questions and / or comments, please feel free to contact me!

+5
source share

All Articles