对于我的实习,我需要使用不需要孩子属性数组的父ID来构建嵌套对象。 我有一个具有ID和父ID的对象数组,我使用npm flatnest来做到这一点。这适用于一级层次结构,但是必须将代码修改为适用于多层次结构级别。 我不知道如何使它适应多层次结构层次。
这是我的Object数组
var fn = require("flatnest");
const flat =
[
{ "id": 1, "name": 'Restaurants', 'parentId': 0},
{ "id": 2, "name": 'family restaurant', 'parentId': 1, 'value':'Excellent'},
{ "id": 3, "name": 'Sun restaurant', 'parentId': 1,'value':""},
{ "id": 4, "name": 'Sun restaurant 1', 'parentId': 3, 'value':'Good'},
{ "id": 5, "name": 'Sun restaurant 2', 'parentId': 3, 'value':"bad"},
{ "id": 6, "name": 'Hotels', 'parentId': 0,'value':""},
{ "id": 7, "name": 'Space Hotel', 'parentId': 6,'value':""},
{ "id": 8, "name": 'Sun Hotel', 'parentId': 7,'value':'Nice'},
{ "id": 9, "name": 'Moon Hotel', 'parentId': 7,'value':""},
{ "id": 10, "name": 'Moon Hotel 1', 'parentId': 9, 'value':"Excellent"},
{ "id": 11, "name": 'Moon Hotel 2', 'parentId': 9, 'value':"Worst"},
];
要使用npm flatnest的嵌套函数,我必须将Object数组(const flat)展平
我的代码扁平化:
var transform={};
for(var i=0;i<flat.length;i++)
{
if(typeof flat[parseInt(i)+1] !== 'undefined' )
{
if(flat[i].id==flat[i+1].parentId)
{
var t = flat[i].name;
transform[t.concat(".").concat(flat[i+1].name)]=flat[i+1].value;
}else{
transform[t.concat(".").concat(flat[i+1].name)]=flat[i+1].value;
}
}
}
console.log(transform)
var nested = fn.nest(transform)
console.log(nested)
我希望console.log(transform)的输出是
{ 'Restaurants.family restaurant':'Excellent',
'Restaurants.Sun restaurant.Sun restaurant 1': 'Good',
'Restaurants.Sun restaurant.Sun restaurant 2': 'bad',
'Hotels.Space Hotel.Sun Hotel': 'Nice',
'Hotels.Space Hotel.Moon Hotel.Moon Hotel 1': 'Excellent',
'Hotels.Space Hotel.Moon Hotel.Moon Hotel 2' : 'Worst'}
然后使用嵌套函数:
var nested = fn.nest(transform)
console.log(nested)
输出必须完全像这样:
"Restaurants":{
"family restaurant":"Excellent",
"Sun restaurant":{
"Sun restaurant 1":"Good",
"Sun restaurant 2":"bad"
}
},
"Hotels":{
"Space Hotel":{
"Sun Hotel":"Nice",
"Moon Hotel":{
"Moon Hotel 1":"Excellent",
"Moon Hotel 2":"Worst"
}
}
}
}
但是console.log(transform)的实际输出是:
{'Restaurants.family restaurant':'Excellent',
'Restaurant.Sun restaurant':'',
'Sun restaurant.Sun restaurant 1':'Good',
'Sun restaurant.Sun restaurant 2':'bad',
'Sun restaurant.Hotels':'',
'Hotels.Space Hotel':'',
'Space Hotel.Sun Hotel':'Nice'
'Space Hotel.Moon Hotel':'',
'Moon Hotel.Moon Hotel 1':'Excellent',
'Moon Hotel.Moon Hotel 2': 'Worst'}
答案 0 :(得分:0)
我没有使用flatnest
。但是下面的代码对我有用。请检查并告知我在任何情况下是否均不起作用。
const flat = [{
"id": 1,
"name": 'Restaurants',
'parentId': 0
},
{
"id": 2,
"name": 'family restaurant',
'parentId': 1,
'value': 'Excellent'
},
{
"id": 3,
"name": 'Sun restaurant',
'parentId': 1,
'value': ""
},
{
"id": 4,
"name": 'Sun restaurant 1',
'parentId': 3,
'value': 'Good'
},
{
"id": 5,
"name": 'Sun restaurant 2',
'parentId': 3,
'value': "bad"
},
{
"id": 6,
"name": 'Hotels',
'parentId': 0,
'value': ""
},
{
"id": 7,
"name": 'Space Hotel',
'parentId': 6,
'value': ""
},
{
"id": 8,
"name": 'Sun Hotel',
'parentId': 7,
'value': 'Nice'
},
{
"id": 9,
"name": 'Moon Hotel',
'parentId': 7,
'value': ""
},
{
"id": 10,
"name": 'Moon Hotel 1',
'parentId': 9,
'value': "Excellent"
},
{
"id": 11,
"name": 'Moon Hotel 2',
'parentId': 9,
'value': "Worst"
},
];
const map = new Map();
const result = flat.reduce((acc, curr) => {
let val = {}
if (curr.parentId == 0)
acc[curr.name] = val;
else {
if (map.get(curr.parentId)) {
if (curr.value != '')
val = curr.value;
map.get(curr.parentId)[curr.name] = val;
}
}
map.set(curr.id, val);
return acc;
}, {});
console.log(JSON.stringify(result));