2017-04-25 142 views
-1

與元素的數組我有一個像下面創建從輸入數組

[{ 
    "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年時把一個「 - 」。 陣列中還會有更多其他參數我需要一些動態邏輯來創建結果數組

+0

有很多方法來實現你想要什麼。但是當你說你想考慮所有元素時,你需要澄清你的真正含義。 – jrook

+0

jrook,我的意思是它應該有所有的參數,但不是像FC_Id.Sub的子陣列,實際上像FC_Id,FC_Name這樣的子數組將被轉換爲單個元素,所以會重複元素的數量。它很難解釋,但你可能會從上面定義的結果數組中得到一個想法。 –

+0

所以一些研究「扁平陣列」。 [這個問題](http://stackoverflow.com/q/40250107/215552)與你非常相似。 –

回答

-1

從哪裏創建此數組。如果您使用JavaScript或從webservice類創建此數組。然後我們可以告訴你它的通用類。還有一個小的方法來轉換你想要的輸出數組中的類元素。

讓我用JavaScript代碼回答。

//我正在創建一個數組對象手冊,您可以從源代碼或從服務器json獲取它。

var Arr=[]; 

var obj={}; 
Obj.Name="fc_name"; 
obj.Value="12"; 
Arr.push(obj); 

var obj={}; 
Obj.Name="fc_value"; 
obj.Value="dieje"; 

Arr.push(obj); 

同樣推送數組中的所有元素。 然後,您將擁有一個具有名稱值的對象數組。

Arr=[ 
{"Name":"fc_value", 
"Value":""dieje" 
}, 
... 
}); 

現在使用下面的函數將這個對象數組轉換爲您的願望數組。

function Convert(arr) 
{ 
var array=[]; 
var obj={}; 
Arr.forEach(value) 
{ 
obj[value.Name]=value.Value; 
} 

array.push(obj); //這是單個對象,您可以應用循環來獲取數組中的多個值。 }

+0

我正在用Javascript做到這一點。 –

0

像這樣:

  • 對於數據
    • 查找FC和CO之間的ID中的至少一個列表(或者如果沒有這樣的構造一個元素列表的每個元素列表中找到)
    • 迭代它的長度,並替換子條目的相關條目
  • 拼合結果

let data = [{"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"}]; 
 
    
 
let result = [].concat(...data.map(r => 
 
    (Array.isArray(r.FC_Id) ? r.FC_Id : 
 
    Array.isArray(r.CO_Id) ? r.CO_Id : 
 
    [0] 
 
).map((_, i) => 
 
    Object.assign({}, r, { 
 
     FC_Id: r.FC_Id ? r.FC_Id[i] : "-", 
 
     FC_Name: r.FC_Name ? r.FC_Name[i] : "-", 
 
     FC_Value: r.FC_Value ? r.FC_Value[i] : "-", 
 
     CO_Id: r.CO_Id ? r.CO_Id[i] : "-", 
 
     CO_Name: r.CO_Name ? r.CO_Name[i] : "-", 
 
     CO_Value: r.CO_Value ? r.CO_Value[i] : "-", 
 
    }) 
 
) 
 
)); 
 

 
console.log(result);

0

這裏是一個片段來實現這一目標。如果您需要值作爲數組來處理更多的性能,簡單的將它們添加到props陣列:

let props = ["FC_Id", "FC_Name", "FC_Value", "CO_Id", "CO_Name", "CO_Value"]

示例代碼段

let arr = [{ 
 
    "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" 
 
    } 
 
] 
 

 
function convertArray() { 
 
    let result = []; 
 
    let props = ["FC_Id", "FC_Name", "FC_Value", "CO_Id", "CO_Name", "CO_Value"] 
 

 
    arr.forEach(function(obj) { 
 
    // Assume `FC_Id` array length is the reference array length 
 
    obj["FC_Id"].forEach(function(id, $index) { 
 
     // Create a clone of original object, so that we only have to override `props` defined above 
 
     let objClone = JSON.parse(JSON.stringify(obj)); 
 
     
 
     // Override each prop value 
 
     props.forEach(function(prop) { 
 
     // Ensure original object property is an array with enough elements 
 
     if (obj[prop] instanceof Array && obj[prop].length > $index) { 
 
      objClone[prop] = obj[prop][$index]; 
 
     } 
 
     else { 
 
      // else simply use an hyphen as value 
 
      objClone[prop] = "-"; 
 
     } 
 
     }); 
 
     
 
     // Push clone to result 
 
     result.push(objClone); 
 
    }); 
 
    }); 
 

 
    return result; 
 
} 
 

 
let newArray = convertArray(); 
 

 
console.log(JSON.stringify(newArray, null, 2));

+0

@AahanaPatel是否回答了您的問題? –