[英]How to add key value pair to object
我有一個包含嵌套對象數組的數據源。 我已經能夠 select 鍵值對,現在我想將這些值添加到 object 的頂層,即嵌套的 object 之外。
初始數組:
data= [
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"properties": [
{
"id": 7080,
"key": "country",
"value": "in",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "hive",
"category": "General"
}
]
},
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"properties": [
{
"id": 7080,
"key": "country",
"value": "au",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "aws",
"category": "General"
}
]
} ]
使用下面的代碼,我可以獲得鍵值對:
for (var i = 0; i < data.length; i++) {
data[i].properties.forEach((arrayItem, i) => {
if (arrayItem.key === 'country') {
console.log('Key: ' + arrayItem.key + ' ' + 'Value: ' + arrayItem.value);
}
});
}
Output 代碼:
Key: country Value: au
Key: country Value: in
如何將這些值推回數組中,以便我的新數組如下所示:
data= [
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"country": "in"
"properties": [
{
"id": 7080,
"key": "country",
"value": "in",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "hive",
"category": "General"
}
]
},
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"country":"au"
"properties": [
{
"id": 7080,
"key": "country",
"value": "au",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "aws",
"category": "General"
}
]
} ]
嘗試使用擴展運算符更新data[i]
:
for (var i = 0; i < data.length; i++) {
data[i].properties.forEach((arrayItem, i) => {
if (arrayItem.key === 'country') {
data[i] = { ...data[i] , arrayItem }
}
});
}
您可以使用Object.fromEntries
:
for (let item of data) {
Object.assign(item,
Object.fromEntries(item.properties.map(({key, value}) => [key, value]))
);
}
如果您只想添加其中一些對,您可以將.filter
到.map
結果,或者如果您只需要一個(“國家”),請使用更基本的編程模式:
for (let item of data) {
item.country = item.properties.find(({key}) => key == "country").value;
}
這將創建一個新數組,其中包含具有附加country
/地區屬性的對象副本(如果找到):
const addCountry = data => data.map (({properties, ...rest}) => { const country = properties.find (({key}) => key == 'country') return {... rest, ... (country? {country: country.value}: {}), properties } }) const data = [{flowId: 7079, flowName: "jackson-demo", version: 1, CreatedDate: "2020-04-02", UpdateDate: "", LastRunDate: "2020-04-02", active: false, properties: [{id: 7080, key: "country", value: "in", category: "General"}, {id: 7081, key: "source", value: "hive", category: "General"}]}, {flowId: 7079, flowName: "jackson-demo", version: 1, CreatedDate: "2020-04-02", UpdateDate: "", LastRunDate: "2020-04-02", active: false, properties: [{id: 7080, key: "country", value: "au", category: "General"}, {id: 7081, key: "source", value: "aws", category: "General"}]}]; console.log ( addCountry (data) )
.as-console-wrapper {min-height: 100%;important: top: 0}
如果此提取已經滿足了 output 中properties
的唯一使用,那么您可以跳過 output 中的properties
行並擁有更多輕量級版本以進行進一步處理。
當然,我們可以選擇改變原始數據,但我們不是野蠻人,對吧?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.