2014-12-25 67 views
4

多級的對象數組我有一個JavaScript數組:簡化在JavaScript

var data = [{abc: "vf",def: [{ a:"1", b:"3"},{a:"2",b:"4"}]}]; 

我希望它轉換爲:

[{"abc":"vf","a":"1","b":"3"},{"abc":"vf","a":"2","b":"4"}] 

我寫它的JavaScript代碼:

Javascript:

var push_apply = Function.apply.bind([].push); 
    var slice_call = Function.call.bind([].slice); 

    Object.defineProperty(Array.prototype, "pushArrayMembers", { 
     value: function() { 
      for (var i = 0; i < arguments.length; i++) { 
       var to_add = arguments[i]; 
       for (var n = 0; n < to_add.length; n+=300) { 
        push_apply(this, slice_call(to_add, n, n+300)); 
       } 
      } 
     } 
    }); 

var globalResultArr = []; 
var data = [{abc: "vf",def: [{ a:"1", b:"3"},{a:"2",b:"4"}]}]; 
function f(){ 
    for(var i=0;i<data.length;i++){ //Iterate Data Array 
     var obj = data[i]; 
     var resultArray = [{}]; 
     for (var key in obj) {   // Iterate Object in Data Array 
      if (obj.hasOwnProperty(key)) { 
       if(Object.prototype.toString.call(obj[key]) === "[object Array]"){ 
        var tempRes = $.extend(true, [], resultArray); 
        resultArray = []; 
        for(var k=0;k<tempRes.length;k++){ 
         var tempResObj = tempRes[k]; 
         for(var j=0;j<obj[key].length;j++){ // If it has array, then iterate it as well 
          var innerObj = obj[key][j]; //Iterate Object in inner array 
          for(var innerkey in innerObj){ 
           if (innerObj.hasOwnProperty(innerkey)) { 
            tempResObj[innerkey] = innerObj[innerkey]; 
           } 
          } 
          resultArray.push(tempResObj); 
         } 
        } 
       }else{ 
        for(var i=0;i<resultArray.length;i++){ 
         resultArray[i][key] = obj[key]; 
        } 
       } 
      } 
     } 
     globalResultArr.pushArrayMembers(resultArray); 
    } 
    document.getElementById("test").innerHTML = JSON.stringify(globalResultArr); 
} 

f(); 

HTML:

<div id="test"></div> 

問題是,瀏覽器崩潰時的數據陣列> 10000項我怎樣才能使這項工作?有沒有更簡單的方法?

JSFiddle

+0

我覺得代碼審查將是這個問題比計算器 – maja

+0

@maja感謝一個更好的地方,將這樣做。 :) –

+0

你想[{「abc」:「vf」,「a」:「1」,「b」:「3」},{「abc」:「vf」,「a」:「2」, 「b」:「4」}]或真正[{「abc」:「vf」,「a」:「2」,「b」:「4」},{「abc」:「vf」 : 「2」, 「b」: 「4」}]? –

回答

2

這裏是一個嘗試。它假定每個對象中只有一個屬性是一個數組。它們用剩餘的屬性組成一個對象,並使用數組屬性中的每個值創建一個新的克隆。
*和現在一樣需要工作*

var resultArray = [], 
    len = data.length, 
    tempObj, 
    newObj, 
    targetVal, 
    targetKey, 
    composeObj, 
    tempJson, 
    key, 
    i, k; 

for(i = 0; i < len; i++) { 
    tempObj = data[i]; 
    // obj to build up 
    composeObj = {}; 
    // look at all key/vals in obj in data 
    for(key in tempObj) { 
     keyObj = tempObj[key]; 
     // is it an array? 
     if(Array.isArray(keyObj)) { 
      // save key/val for array property 
      targetVal = keyObj; 
      targetKey = key; 
     } 
     else { 
      // copy non-array properties to empty object 
      composeObj[key] = keyObj; 
     } 
    } 
    // json-ify the object for cloning 
    tempJson = JSON.stringify(composeObj); 
    // compose new object for each val in array property 
    targetVal.map(function(val) { 
     // clone object 
     newObj = JSON.parse(tempJson); 
     // add array values as object properties 
     for(k in val) { 
      newObj[k] = val[k]; 
     } 
     // add to results 
     resultArray.push(newObj); 
    }); 
}