[英]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.