简体   繁体   中英

javaScript map/reduce on linked objects

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM