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.
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.
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 Dozer is being configured.
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?
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
Object properties can be both primitive values, other objects, and functions. An object method is an object property containing a function definition. JavaScript objects are containers for named values, called properties and methods. You will learn more about methods in the next chapters.
There are two types of object properties: The data property and the accessor property. Each property has corresponding attributes. Each attribute is accessed internally by the JavaScript engine, but you can set them through Object.
Nested objects are objects that are inside another object. You can create nested objects within a nested object. In the following example, Salary is an object that resides inside the main object named Employee . The dot notation can access the property of nested objects. JavaScript.
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.
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.
I hope this library suits your needs. Should you have any questions and/or remarks, please don't hesitate contacting me!
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/
Actually, the knockoutjs mapping plugin allows you to do just that:
In the ko.mapping.fromJS
call you can provide a mapping object that will be used to map the containing properties...
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.
You can read more about this feature in the official documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With