與元素的數組我有一個像下面創建從輸入數組
[{
"IM_Id":1,
"IM_Name":"Hello",
"ItemNumber":2001,
"FE_Id":2,
"FE_Name":"Wall",
"FE_Code":"XYZ",
"FC_Id":[2,3],
"FC_Name":["ABC","PQR"],
"FC_Value":[9,7],
"OC_Id":6,
"OC_Name":"Rai",
"OC_Price":"$30",
"CH_Id":4,
"CH_Name":"Sen",
"CH_Code":"LMN",
"CO_Id":[5,9],
"CO_Name":["xyz","pqr"],
"CO_Value":["qqq","LMN"]
},
{
"IM_Id":2,
"IM_Name":"World",
"ItemNumber":2002,
"FE_Id":3,
"FE_Name":"WallMart",
"FE_Code":"009F",
"FC_Id":[4,5],
"FC_Name":["ABCD","PQRS"],
"FC_Value":[4,2],
"OC_Id":7,
"OC_Name":"Raj",
"OC_Price":"$60",
"CH_Id":7,
"CH_Name":"Ken",
"CH_Code":"IJK"
}]
可以看到FC_Id,FC_Name,FC_Value有2種元素和這3個參數,實際涉及的數組。類似地,所有用CO預置的參數都具有相同的模式。
我想我的結果數組應該考慮數組的每個元素一個接一個。
因此,對於數組的第一個元素,它應該考慮FC_Id,FC_Name,FC_Value,CO_Id,CO_Name和CO_Value中的一個元素。
它實際上應該考慮到所有的子陣列和主陣列的元素。 因此我的結果應該是如下
[{
"IM_Id":1,
"IM_Name":"Hello",
"ItemNumber":2001,
"FE_Id":2,
"FE_Name":"Wall",
"FE_Code":"XYZ",
"FC_Id":2, //Here it took the first element
"FC_Name":"ABC", // //Here it took the first element
"FC_Value":9, ////Here it took the first element
"OC_Id":6,
"OC_Name":"Rai",
"OC_Price":"$30",
"CH_Id":4,
"CH_Name":"Sen",
"CH_Code":"LMN",
"CO_Id":5,
"CO_Name":"xyz",
"CO_Value":"qqq"
},
{
"IM_Id":1,
"IM_Name":"Hello",
"ItemNumber":2001,
"FE_Id":2,
"FE_Name":"Wall",
"FE_Code":"XYZ",
"FC_Id":3, //Here it took the second element
"FC_Name":"PQR", //Here it took the second element
"FC_Value":7,//Here it took the second element
"OC_Id":6,
"OC_Name":"Rai",
"OC_Price":"$30",
"CH_Id":4,
"CH_Name":"Sen",
"CH_Code":"LMN",
"CO_Id":9,
"CO_Name":"pqr",
"CO_Value":"LMN"
},
{
"IM_Id":2,
"IM_Name":"World",
"ItemNumber":2002,
"FE_Id":3,
"FE_Name":"WallMart",
"FE_Code":"009F",
"FC_Id":4,
"FC_Name":"ABCD",
"FC_Value":4,
"OC_Id":7,
"OC_Name":"Raj",
"OC_Price":"$60",
"CH_Id":7,
"CH_Name":"Ken",
"CH_Code":"IJK",
"CO_Id":"-",
"CO_Name":"-",
"CO_Value":"-"
},
{
"IM_Id":2,
"IM_Name":"World",
"ItemNumber":2002,
"FE_Id":3,
"FE_Name":"WallMart",
"FE_Code":"009F",
"FC_Id":5,
"FC_Name":"PQRS",
"FC_Value":2,
"OC_Id":7,
"OC_Name":"Raj",
"OC_Price":"$60",
"CH_Id":7,
"CH_Name":"Ken",
"CH_Code":"IJK",
"CO_Id":"-", // as there's no element here we are inputting a '-'
"CO_Name":"-",
"CO_Value":"-"
}
]
另外請注意,由於沒有CO_值項目編號2002年時把一個「 - 」。 陣列中還會有更多其他參數我需要一些動態邏輯來創建結果數組
有很多方法來實現你想要什麼。但是當你說你想考慮所有元素時,你需要澄清你的真正含義。 – jrook
jrook,我的意思是它應該有所有的參數,但不是像FC_Id.Sub的子陣列,實際上像FC_Id,FC_Name這樣的子數組將被轉換爲單個元素,所以會重複元素的數量。它很難解釋,但你可能會從上面定義的結果數組中得到一個想法。 –
所以一些研究「扁平陣列」。 [這個問題](http://stackoverflow.com/q/40250107/215552)與你非常相似。 –