遍历一些结果以找到路径并将其推送到数组
Loop through some results to find a path and push it to an array
我需要创建一个循环来搜索一些结果以找到路径并将其推入数组,但我编写循环的方式只搜索第一个位置,即 [0]。
如何循环所有结果位置以便从所有位置中提取路径?
为了更好的理解,请查看下面的图片和代码:
到目前为止的代码如下所示:
let results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
//loop on each results
results[0].Cells.results.forEach(el => {
let filePath = el.value;
let fileName = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length);
// push to array
pathResults.push(fileName);
});
提前感谢您的任何建议!
const pathList = data
.d.query
.PrimaryQueryResult
.RelevantResults
.Table.Rows
.results
.reduce((list, { Cells }) => {
const cellPathItem = Cells
.results.find(({ Key }) => Key === 'Path');
if (cellPathItem) {
list.push(
cellPathItem.Value.substring(
cellPathItem.Value.lastIndexOf('/')
)
);
}
return list;
}, []);
"Thanks for your sophisticated solution! Could you please show me an approach based more on my existing code?"
const pathList = [];
data.d.query
.PrimaryQueryResult
.RelevantResults
.Table.Rows
.results
.forEach(rowItem => {
const cellPathItem = rowItem.Cells
.results.find(cellItem => cellItem.Key === 'Path');
if (cellPathItem) {
pathList.push(
cellPathItem.Value.substring(
cellPathItem.Value.lastIndexOf('/')
)
);
}
});
我需要创建一个循环来搜索一些结果以找到路径并将其推入数组,但我编写循环的方式只搜索第一个位置,即 [0]。
如何循环所有结果位置以便从所有位置中提取路径?
为了更好的理解,请查看下面的图片和代码:
到目前为止的代码如下所示:
let results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
//loop on each results
results[0].Cells.results.forEach(el => {
let filePath = el.value;
let fileName = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length);
// push to array
pathResults.push(fileName);
});
提前感谢您的任何建议!
const pathList = data
.d.query
.PrimaryQueryResult
.RelevantResults
.Table.Rows
.results
.reduce((list, { Cells }) => {
const cellPathItem = Cells
.results.find(({ Key }) => Key === 'Path');
if (cellPathItem) {
list.push(
cellPathItem.Value.substring(
cellPathItem.Value.lastIndexOf('/')
)
);
}
return list;
}, []);
"Thanks for your sophisticated solution! Could you please show me an approach based more on my existing code?"
const pathList = [];
data.d.query
.PrimaryQueryResult
.RelevantResults
.Table.Rows
.results
.forEach(rowItem => {
const cellPathItem = rowItem.Cells
.results.find(cellItem => cellItem.Key === 'Path');
if (cellPathItem) {
pathList.push(
cellPathItem.Value.substring(
cellPathItem.Value.lastIndexOf('/')
)
);
}
});