javaScript map/reduce 在链接对象上
javaScript map/reduce on linked objects
我几乎没有像数据库中的表那样相互链接的对象:
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"
},
]
}
};
而且我必须创建一个包含所有链接数据的对象,如下所示:
"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"
}
}
现在我有丑陋的大循环,它不喜欢这是连接对象的最佳方式。也许我可以使用 map/reduce 函数来自动化我需要的连接?
下面的代码生成一个对象数组
//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;
})
现在,我不确定您需要的最终格式。但是如果你想把所有这些都放在一个对象中,你可以这样写:
var resultObj = {'result' : resultArray };
但是,除非您对 resultObj 中的项目有适当有意义的名称,否则我建议坚持使用数组并遍历数组的数字索引。
希望对您有所帮助
我几乎没有像数据库中的表那样相互链接的对象:
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"
},
]
}
};
而且我必须创建一个包含所有链接数据的对象,如下所示:
"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"
}
}
现在我有丑陋的大循环,它不喜欢这是连接对象的最佳方式。也许我可以使用 map/reduce 函数来自动化我需要的连接?
下面的代码生成一个对象数组
//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;
})
现在,我不确定您需要的最终格式。但是如果你想把所有这些都放在一个对象中,你可以这样写:
var resultObj = {'result' : resultArray };
但是,除非您对 resultObj 中的项目有适当有意义的名称,否则我建议坚持使用数组并遍历数组的数字索引。
希望对您有所帮助