所以我有這樣的陣列從JSON獲取陣列字段名稱
data : [{
article_categories : {
id : "xxx",
name : "xxx"
},
article : "xxx",
publisher : "xxx"
}]
我想爲那些陣列另一個多維數組,我想保留的字段名(名字"article"
,"publisher"
等在那裏的陣列)與價值,但我不知道讓字段名稱
,我也希望做一些有條件的,如果只包括某些領域成爲我的新陣列通過從該陣列檢查
thead: [
{ key: "article"},
{ key: "article_categories.name"},
.....
]
,所以我最終會有這樣
newArray: [
{article:"xxx",publisher: "xxx",article_categories.name:"xxx"},
{article:"xxx",publisher: "xxx",article_categories.name:"xxx"}
....
]
陣列如何做到這一點?我試圖
thead.forEach(function(column){
data.forEach(function(key,value){
if(column.key == key){
newArray[key] = value
}
})
})
,但它只是不工作....
,需要加載一個70KB的
keys
(精縮!)庫只是遍歷數組。 ..:/ –以及我已經使用lodash所以我不介意,但對不起,我有另一個問題,但仍然與此有關...如果我的thead json數組是一個對象,並且像這樣:this:{article:「string」,publisher:「string」,....}如何修改這些代碼? –
@LaurensiusTony - 在這種情況下,您只會對對象的關鍵點感興趣。所以,你可以用'_.keys(tdata).forEach(...)'替換'keys.forEach'。 – 31piy