假設我有這樣的資料
let data = [
{
subPoint:[
{
point:'point a 1.1',
},
{
point:'point a 1.2',
},
{
subPoint:[
{
subPoint:[
{
point:'point a 1.3.1.1'
}
]
},
{
point:'point a 1.3.1.2'
}
]
},
]
},
{
point:'point b 1'
},
{
point:'point c 1'
},
{
subPoint:[
{
subPoint:[
{
point:'point d 1.1.1'
}
]
}
]
}
]
我的預期結果應該是這樣的
[
'1.1.1---point a 1.1',
'1.1.2---point a 1.2',
'1.1.3.1.1---point a 1.3.1.1',
'1.1.3.1.2---point a 1.3.1.2',
'2.1---point b 1',
'3.1---point c 1',
'4.1.1.1---point d 1.1.1'
]
但我得到的是這個
[
'1.3.1---point a 1.1',
'1.3.2---point a 1.2',
'1.3.3.1.1.1---point a 1.3.1.1',
'1.3.3.1.2---point a 1.3.1.2',
'2---point b 1',
'3---point c 1',
'4.1.1.1---point d 1.1.1'
]
我的代碼看起來像這樣
const getInfo = (starIndex,array) => {
let rowId = starIndex
array.forEach((val,ind) => {
if(val.subPoint){
console.log(starIndex)
rowId = starIndex '.' (ind 1)
return getInfo(rowId,val.subPoint)
}
})
console.log('rowId',rowId)
return rowId
}
let returnData = []
const getData = (_data,val) => {
_data.forEach((_dat,index) => {
let value = (val?`${val}.`:'') `${index 1}`
if(_dat.subPoint){
value = getInfo(value,_dat.subPoint)
getData(_dat.subPoint,value)
}else {
returnData.push(value '---' _dat.point)
}
})
return returnData
}
console.log(getData(data))
我想我錯過了一些東西或者我的遞回很糟糕我不確定問題是什么,
這是一個問題的隱喻代碼,我遇到的實際問題是為每個可以有任意數量的分組的表行組件提供一個唯一的 ID。
uj5u.com熱心網友回復:
沒有額外的水平。
const
getFlat = (parent = '') => ({ point, subPoint = [] }, i) => {
const
p = parent (parent && '.') (i 1),
children = subPoint.flatMap(getFlat(p));
if (point) children.unshift([p, point].join('---'));
return children;
},
data = [{ subPoint: [{ point: 'point a 1.1' }, { point: 'point a 1.2' }, { subPoint: [{ subPoint: [{ point: 'point a 1.3.1.1' }] }, { point: 'point a 1.3.1.2' }] }] }, { point: 'point b 1' }, { point: 'point c 1' }, { subPoint: [{ subPoint: [{ point: 'point d 1.1.1' }] }] }],
result = data.flatMap(getFlat());
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
const data = [
{
subPoint: [
{
point: 'point a 1.1',
},
{
point: 'point a 1.2',
},
{
subPoint: [
{
subPoint: [
{
point: 'point a 1.3.1.1',
},
],
},
{
point: 'point a 1.3.1.2',
},
],
},
],
},
{
point: 'point b 1',
},
{
point: 'point c 1',
},
{
subPoint: [
{
subPoint: [
{
point: 'point d 1.1.1',
},
],
},
],
},
];
const flatten = (data) => {
let result = [];
const recurse = (data, path = '') => {
for (let i = 0; i < data.length; i ) {
let item = data[i];
let newPath = path ? `${path}.${i 1}` : `${i 1}`;
if (item.point) {
result.push(`${newPath}---${item.point}`);
} else {
recurse(item.subPoint, newPath);
}
}
};
recurse(data);
return result;
}
console.log(flatten(data));
uj5u.com熱心網友回復:
此輸出與您的請求不匹配,但對我來說似乎更合乎邏輯:
[
"1.1---point a 1.1",
"1.2---point a 1.2",
"1.3.1.1---point a 1.3.1.1",
"1.3.2---point a 1.3.1.2",
"2---point b 1",
"3---point c 1",
"4.1.1---point d 1.1.1"
]
如果這對你有用,這里有一個簡單的遞回來生成它:
const outline = (xs, path = []) => xs .flatMap (({point, subPoint = []}, i) => point
? [`${path .concat (i 1) .join ('.')}---${point}`]
: outline (subPoint, path .concat (i 1))
)
const data = [{subPoint: [{point: "point a 1.1"}, {point: "point a 1.2"}, {subPoint: [{subPoint: [{point: "point a 1.3.1.1"}]}, {point: "point a 1.3.1.2"}]}]}, {point: "point b 1"}, {point: "point c 1"}, {subPoint: [{subPoint: [{point: "point d 1.1.1"}]}]}]
console .log (outline (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
我們跟蹤一個運行路徑,它可能包含諸如[2]or之類的值[1, 3, 1, 1],并且在每次呼叫時,如果我們點擊了具有point屬性的節點,我們會將路徑與該屬性字串結合起來。如果還沒有,我們將當前索引(加一以處理 JS 的從零開始計數)添加到路徑中,并在節點的子subPoint節點上遞回。
如果該輸出不起作用,您能否準確解釋每個目標大綱前綴的來源?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442124.html
標籤:javascript 数组 反应 递归 递归数据结构
