我有以下JSON数组:
[
{ id: 1, name: "P1", group_id: 1.1, group_name: "G1.1"},
{ id: 1, name: "P1", group_id: 1.2, group_name: "G1.2"},
{ id: 2, name: "P2", group_id: 1.1, group_name: "G2.1"},
{ id: 2, name: "P2", group_id: 1.1, group_name: "G2.2"},
]
使用Javascript ES6将其转换为以下结构的最有效方法是什么?
dict(zip(col1,col2))
答案 0 :(得分:1)
有一些问题。
def apply_python_func(ds, func, sess):
"""Exact values from ds using sess and apply func on them"""
it = ds.make_one_shot_iterator()
next_value = it.get_next()
num_examples = 0
while True:
try:
value = sess.run(next_value)
num_examples += 1
func(value)
except tf.errors.OutOfRangeError:
break
print('Evaluated {} examples'.format(num_examples))
you can validate json arrays using this link
设置数组后,您可以使用var obj = [{
"id": 1,
"name": "P1",
"groups": [{
"id": 1.1,
"name": "G1.1"
}, {
"id": 1.2,
"name": "G1.2"
}]
},
{
"id": 2,
"name": "P2",
"groups": [{
"id": 2.1,
"name": "G2.1"
}, {
"id": 2.2,
"name": "G2.2"
}]
}
];
。
ForEach

答案 1 :(得分:0)
您可以使用forEach
来实现此目的。
请尝试以下方法:
var arr=[
{ id: 1, name: "P1", groups: [ { id: 1.1, name: "G1.1" }, { id: 1.2, name:"G1.2" }]},
{ id: 2, name: "P2", groups: [ { id: 2.1, name: "G2.1" }, { id: 2.2, name:"G2.2" }]}
];
result = [];
arr.forEach((o)=>{
o.groups.forEach((group)=>{
result.push({
"id" : o.id,
"name" : o.name,
"group_id" : group.id,
"group_name" : group.name
});
});
});
console.log(result);