我想将输入JSON转换为对象的输出数组,使用输入JSON数组,只使用普通的Javascript,循环通过JSON对象。我尝试了foreach函数,但遇到了一些问题。打印输出如下方所示
let Input ={
details:[
{
"id":"Country_name",
"values":[
"India",
"England",
"Germany"
]
},
{
"id":"Country_capital",
"values":[
"Delhi",
"London",
"Berlin"
]
}
],
metadata:[
{
"id":"Country_name",
"label":"Country"
},
{
"id":"Country_capital",
"label":"Capital"
}
]
}
let Output =[
{
"Country":"India",
"Capital":"Delhi"
},
{
"Country":"England",
"Capital":"London"
},
{
"Country":"Germany",
"Capital":"Berlin"
}
]
Object.keys(input).forEach(function(value, key) {
input[value].forEach(function(v, k) {
console.log(v.id)
})
})
您可以尝试类似的
const input = {
details: [{
"id": "Country_name",
"values": [
"India",
"England",
"Germany"
]
},
{
"id": "Country_capital",
"values": [
"Delhi",
"London",
"Berlin"
]
}
],
metadata: [{
"id": "Country_name",
"label": "Country"
},
{
"id": "Country_capital",
"label": "Capital"
}
]
};
function transform(input) {
const ids = {};
for (const detail of input.details) {
ids[detail.id] = detail.values;
}
const meta = {};
for (const m of input.metadata) {
meta[m.id] = m.label;
}
const idsKeys = Object.keys(ids);
const out = [];
for (let i = 0; i < ids[idsKeys[0]].length; i++) {
const obj = {};
for (const key of idsKeys) {
obj[meta[key]] = ids[key][i];
}
out.push(obj);
}
return out;
}
console.log(transform(input));
我更改了输入和顶部中的变量
这可能适用于您,也可能不适用于
let input ={
details:[
{
"id":"Country_name",
"values":[
"India",
"England",
"Germany"
]
},
{
"id":"Country_capital",
"values":[
"Delhi",
"London",
"Berlin"
]
}
],
metadata:[
{
"id":"Country_name",
"label":"Country"
},
{
"id":"Country_capital",
"label":"Capital"
}
]
}
let Output =[
{
"Country":"India",
"Capital":"Delhi"
},
{
"Country":"England",
"Capital":"London"
},
{
"Country":"Germany",
"Capital":"Berlin"
}
]
Object.keys(input).forEach(function(value, key) {
input[value].forEach(function(v, k) {
console.log(v.id)
})
})
如果你还有任何问题,我很乐意帮助
- 国家/地区位于
Input.details[0].values
- 资本在
Input.details[1].values
只需在一次通过的国家中创建一个包含国家的对象,然后在第二次通过的首都中为每个国家添加资本
let Input = {
details: [{
"id": "Country_name",
"values": [
"India",
"England",
"Germany"
]
},
{
"id": "Country_capital",
"values": [
"Delhi",
"London",
"Berlin"
]
}
],
metadata: [{
"id": "Country_name",
"label": "Country"
},
{
"id": "Country_capital",
"label": "Capital"
}
]
};
let Output = [{
"Country": "India",
"Capital": "Delhi"
},
{
"Country": "England",
"Capital": "London"
},
{
"Country": "Germany",
"Capital": "Berlin"
}
];
const newOutput = [];
const countries = Input.details[0].values;
countries.forEach(country => {
console.log(country);
newOutput.push({"Country": country});
});
const capitals = Input.details[1].values;
capitals.forEach((capital, i) => {
console.log(capital);
newOutput[i]["Capital"] = capital;
});
console.log(newOutput);
实现这一点的一种简写方法是映射input
:的数据数组
let input ={
details:[
{
"id":"Country_name",
"values":[
"India",
"England",
"Germany"
]
},
{
"id":"Country_capital",
"values":[
"Delhi",
"London",
"Berlin"
]
}
],
metadata:[
{
"id":"Country_name",
"label":"Country"
},
{
"id":"Country_capital",
"label":"Capital"
}
]
}
let Output =[
{
"Country":"India",
"Capital":"Delhi"
},
{
"Country":"England",
"Capital":"London"
},
{
"Country":"Germany",
"Capital":"Berlin"
}
]
let data = input.details
let countries = data[0].values
let capitals = data[1].values
const output = countries.map((el, index) => ({"country": el, "capital": capitals[index]}))
console.log(output)
但是,在您的代码中,您将Input
对象称为input
,这是两个不同的变量