我有對象的數組是這樣的:如何分割使用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但它是沒有用的。
這是一個降低。爲此使用_.reduce()。 –
你的意思是:1和2代替aa和ab,對嗎? – mplungjan
對不起,是的,我想念@mplungjan – waskito