2015-06-04 148 views
5

我有對象的數組是這樣的:如何分割使用lodash特定條件對象的JavaScript數組/ underscorejs

var data = [ 
{ 
    type : "parent", 
    name : "A" 
}, 
{ 
    type : "child", 
    name : "1" 
}, 
{ 
    type : "child", 
    name : "2" 
}, 
{ 
    type : "parent", 
    name : "B" 
}, 
{ 
    type : "child", 
    name : "3" 
} 
] 

,我想移動子對象到父對象,由parrent對象分裂(有沒有給出來自子對象的鍵屬於哪個父類)。所以它只是由父對象分開。簡單地說我想數組變成:

[ 
    { 
    type : "parent", 
    name : "A", 
    child: [ 
     { 
      type : "child", 
      name : "1" 
     }, 
     { 
      type : "child", 
      name : "2" 
     } 
    ] 
    }, 
    { 
    type : "parent", 
    name : "B", 
    child: [ 
     { 
      type : "child", 
      name : "3" 
     } 
     ] 
    } 
] 

我已閱讀lodash約chunk但它是沒有用的。

+0

這是一個降低。爲此使用_.reduce()。 –

+1

你的意思是:1和2代替aa和ab,對嗎? – mplungjan

+0

對不起,是的,我想念@mplungjan – waskito

回答

8

您可以使用本機Array.prototype.reduce功能或lodash的reduce

var data = [ 
{ 
    type : "parent", 
    name : "A" 
}, 
{ 
    type : "child", 
    name : "1" 
}, 
{ 
    type : "child", 
    name : "2" 
}, 
{ 
    type : "parent", 
    name : "B" 
}, 
{ 
    type : "child", 
    name : "3" 
} 
]; 

// If using _.reduce then use: 
// var newData = _.reduce(data, function(arr, el) {...}, []); 
var newData = data.reduce(function(arr, el) { 
    if (el.type === 'parent') { 
    // If el is pushed directly it would be a reference 
    // from the original data object 
    arr.push({type: el.type, name: el.name, child: []}); 
    } 
    else { 
    arr[arr.length - 1].child.push({type: el.type, name: el.name}); 
    } 

    return arr; 
}, []); 
+0

謝謝@JasonCust。你太棒了! – waskito

0

普通的JavaScript版本:

var newArr = []; 
var j=0; 
var k=0; 
for (var i = 0; i <data.length; i++) { 
    if(data[i].type == 'parent'){ 
     newArr[j] = data[i]; 
     newArr[j].children = []; 
     j++; 
     k=0; 
    } 
    else { 
     data[i].name = newArr[j-1].name.toLowerCase() + String.fromCharCode(k + 97) 
     newArr[j-1].children[k] =data[i]; 
     k++; 
    } 
} 
console.log(newArr) 

我在這裏假設父總是放在孩子面前,爲您的示例中的數據提供。

此外,如果您可以防止26歲以上的孩子的父母,這將是一件好事。這會導致String.fromCharCode(k + 97)打印奇怪的字符。爲此,請參見http://www.asciitable.com/

2

這是一個lodash解決方案,可能有點容易理解。 CodePen

的幾個注意事項:

  • 這個修改傳入的數據對象 - 如果這就是我們可以在一些_.clone()電話折騰的一個問題。
  • 這如果每個家長有26個或更少的孩子只會工作,因爲你挑
name: "ab"格局,
var lastParent; 
var result = _.chain(data) 
    .groupBy(function (item) { 
    if (item.type === 'parent') lastParent = item.name 
    return lastParent 
    }) 
    .map(function (group) { 
    var parent = _.first(group) 
    parent.child = _.chain(group) 
     .slice(1) 
     .map(function (child, index) { 
     child.name = parent.name.toLowerCase() + String.fromCharCode(index + 97) 
     return child 
     }) 
     .value() 
    return parent 
    }) 
    .value() 

console.log(result) 
0
for (ele in data) 
{ 
    if (!data[ele].hasOwnProperty('child') && data[ele].type=='parent') 
    { 
     data[ele].child = []; 
     while(data[parseInt(ele) + 1] && data[parseInt(ele) + 1].type == 'child') 
     { 
      data[ele].child.push({type: data[parseInt(ele) + 1].type, name:data[parseInt(ele) + 1].name}); 
      data.splice(parseInt(ele) + 1, 1); 
     } 
    } 
} 
console.log(data); 
0

嘗試簡單循環:

var current, parent, result = [], i = 0; 

while(current = data[i++]){ 

    if(current.type === "parent"){ 
     current.child = []; 
     result.push(current); 
     parent = current 
    }else{ 
     current.name = (parent.name + String.fromCharCode(parent.child.length + 97)).toLowerCase(); 
     parent.child.push(current) 
    } 

} 

Demo

相關問題