1

I have few objects that are linked between each other like a tables in database:

var data = {}
data.Contracts = {
    "RefferencesObject": {
        "Refferencs": [
            {                
                "amount": 0,
                "refState": "a",               
                "refference": "REF1"
            },

            {                
                "amount": 850,
                "refState": "a",               
                "refference": "REF2"
            },
            {                
                "amount": 2000,
                "refState": "a",               
                "refference": "REF3"
            }            
        ]
    },
    "CardsObject": {
        "Cards": [
            {               
                "refference": "REF1",
                "card": "0001"
            },
            {               
                "refference": "REF2",
                "card": "0002"
            },
            {                
                "refference": "REF2",
                "card": "0003"
            },
            {                
                "refference": "REF2",
                "card": "0004"
            },
            {                
                "refference": "REF3",
                "card": "0003"
            },
            {                
                "refference": "REF3",
                "card": "0005"
            }
        ]
    },
    "CardsStatesObject": {
        "CardsStates": [
            {                
                "cardState": "active",                
                "card": "0001"
            },
            {

                "cardState": "closed",               
                "card": "0002"
            },
            {

                "cardState": "closed",               
                "card": "0003"
            },
            {

                "cardState": "active",               
                "card": "0004"
            },
            {

                "panState": "closed",               
                "pan": "0005"
            },
        ]
    }
};

And I have to create an object that will contain all linked data, like this:

"ResultObject" : {
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF1",
        "card" : "0001",
        "cardState" : "active"
    },
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF2",
        "card" : "0002",
        "cardState" : "closed"
    },
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF2",
        "card" : "0003",
        "cardState" : "closed"
    },
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF2",
        "card" : "0004",
        "cardState" : "closed"
    },
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF3",
        "card" : "0003",
        "cardState" : "active"
    },
    {
        "amount": 0,
        "refState": "a",               
        "refference": "REF3",
        "card" : "0005",
        "cardState" : "active"
    }
}

Now I have big ugly loops and it doesn't like like it's the best way to join the objects. Maybe I can use map/reduce functions to automate the join I need?

Daria
  • 861
  • 11
  • 29
  • 1
    your desired result is an invalid javascript object, so no wonder you can't create it – Jaromanda X Aug 17 '15 at 12:32
  • @JaromandaX Why is it invalid? I could mistake but I have a goal just to show what I want to reach – Daria Aug 17 '15 at 12:35
  • general form of a javascript object: `{ nameValuePair1, nameValuePair2, ...nameValuePairN }` ... `nameValuePair` - Pairs of names (strings) and values (any value) where the name is separated from the value by a colon. - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object – Jaromanda X Aug 17 '15 at 12:39
  • 1
    You'll find good answers here: [Is there some way I can “join” the contents of two javascript arrays much like I would do a join in SQL](http://stackoverflow.com/questions/17500312/is-there-some-way-i-can-join-the-contents-of-two-javascript-arrays-much-like-i) – gfullam Aug 17 '15 at 13:44

1 Answers1

1

The below code generates an array of objects

 //I make some references to make coding easier and lines concise.
 References = data.Contracts.RefferencesObject.Refferencs;
 cardsObjects = data.Contracts.CardsObject.Cards;
 cardsStates =  data.Contracts.CardsStatesObject.CardsStates;

resultArray = References.map(function(item, index){
  for (i=0;i<cardsObjects.length;i++){
    if (cardsObjects[i].refference==item.refference){
        item.card = cardsObjects[i].card;
        for (j=0; j < cardsStates.length;j++){
            if (cardsStates[j].card==item.card){
                item.cardState=cardsStates[j].cardState;
            }

        }
    }
  }
  return item;
})

Now, I am not sure about the final format you need. But if you want to have all of this in an object you can just write:

var resultObj = {'result' : resultArray };

However, unless you have proper meaningful names for the items in your resultObj, then I would recommend to stick by an array and iterate through numerical indices of the array.

Hope this helps

maaeab
  • 326
  • 1
  • 7