Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What JavaScript object to object mapping libraries exist? [closed]

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?

like image 613
thomaux Avatar asked May 21 '12 11:05

thomaux


People also ask

What are JavaScript Map objects?

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.

What are the objects available in JavaScript?

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.

How many types of objects are there in JavaScript?

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.

What is nested object in JavaScript?

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.


4 Answers

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!

like image 50
DotBert Avatar answered Sep 30 '22 14:09

DotBert


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/

like image 39
Florian Margaine Avatar answered Oct 02 '22 14:10

Florian Margaine


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.

like image 32
Samir Hafez Avatar answered Sep 29 '22 14:09

Samir Hafez


  1. 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.

  1. 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.
like image 44
IKriKan Avatar answered Oct 03 '22 14:10

IKriKan