使用 Javascript 将数组转换为嵌套对象结构
Convert array to nested object structure using Javascript
我正在尝试使用 Javascript
将一个对象数组转换为一个嵌套对象结构,但结果并不如预期。我在下面解释我的代码。
let data = [
{
"hop_no": 1,
"jumphost_id": "jumphost1"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost"
}
]
let result ='';
for(let i = 0; i< data.length; i++) {
const index = i+1;
const obj = data.find(o => o.name === index);
if(result === '' && !result['host-id']) {
result = {
"host-id": obj.jumphost_id,
"next-hop":{}
}
}else{
if(result['next-hop'] === '') {
result['next-hop'] = obj.jumphost_id
}
}
}
这里给出了对象数组i.e- data
,下面给出了我预期的结构。
低于预期结果。
{
"host-id": "jumphost1",
"next-hop": {
"host-id": "jumphost2",
"next-hop": {
"host-id": "another-jumphost",
"next-hop": {
"host-id": "last-jumphost",
}
}
}
}
这里的条件是当 "hop_no": 1
相应的 jumphost_id
值将分配给 host-id
并且将成为第一个键。之后,嵌套对象结构将按 hop_no
值的升序排列,相应的 jumphost_id
值将分配给 next-hop
键。任何人都可以帮助我仅使用 Javascript 设计此结构。
您可以尝试从内到外构建对象。
像这样:
let data = [
{
"hop_no": 1,
"jumphost_id": "jumphost1"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost"
}
]
let result = {};
for (var i = data.length - 1; i >= 0; i--) {
let tmp = {
'host-id': data[i]['jumphost_id'],
};
if (Object.keys(result).length !== 0) {
tmp['next-hop'] = result;
}
result = tmp;
}
console.log(result);
您可以使用 reduce
函数执行此操作:
let data = [{
"hop_no": 1,
"jumphost_id": "jumphost1",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
}
]
let result = data.sort((a, b) => b.hop_no - a.hop_no).reduce((prev, curr, i, array) => {
let { hop_no, jumphost_id, ...rest } = curr
return {
'host-id': jumphost_id,
...rest,
...(i > 0 ? {'next-hop': { ...prev }} : {}),
}
}, {})
console.log(result)
我正在尝试使用 Javascript
将一个对象数组转换为一个嵌套对象结构,但结果并不如预期。我在下面解释我的代码。
let data = [
{
"hop_no": 1,
"jumphost_id": "jumphost1"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost"
}
]
let result ='';
for(let i = 0; i< data.length; i++) {
const index = i+1;
const obj = data.find(o => o.name === index);
if(result === '' && !result['host-id']) {
result = {
"host-id": obj.jumphost_id,
"next-hop":{}
}
}else{
if(result['next-hop'] === '') {
result['next-hop'] = obj.jumphost_id
}
}
}
这里给出了对象数组i.e- data
,下面给出了我预期的结构。
低于预期结果。
{
"host-id": "jumphost1",
"next-hop": {
"host-id": "jumphost2",
"next-hop": {
"host-id": "another-jumphost",
"next-hop": {
"host-id": "last-jumphost",
}
}
}
}
这里的条件是当 "hop_no": 1
相应的 jumphost_id
值将分配给 host-id
并且将成为第一个键。之后,嵌套对象结构将按 hop_no
值的升序排列,相应的 jumphost_id
值将分配给 next-hop
键。任何人都可以帮助我仅使用 Javascript 设计此结构。
您可以尝试从内到外构建对象。
像这样:
let data = [
{
"hop_no": 1,
"jumphost_id": "jumphost1"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost"
}
]
let result = {};
for (var i = data.length - 1; i >= 0; i--) {
let tmp = {
'host-id': data[i]['jumphost_id'],
};
if (Object.keys(result).length !== 0) {
tmp['next-hop'] = result;
}
result = tmp;
}
console.log(result);
您可以使用 reduce
函数执行此操作:
let data = [{
"hop_no": 1,
"jumphost_id": "jumphost1",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 2,
"jumphost_id": "jumphost2",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 3,
"jumphost_id": "another-jumphost",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
},
{
"hop_no": 4,
"jumphost_id": "last-jumphost",
ip: "",
port: 22,
user: "",
password: "",
"device-type": "linux"
}
]
let result = data.sort((a, b) => b.hop_no - a.hop_no).reduce((prev, curr, i, array) => {
let { hop_no, jumphost_id, ...rest } = curr
return {
'host-id': jumphost_id,
...rest,
...(i > 0 ? {'next-hop': { ...prev }} : {}),
}
}, {})
console.log(result)