简体   繁体   中英

Loop through some results to find a path and push it to an array

I need to create a loop to search into some results to find a path and push it into an array but the way I wrote the loop, it searches only the first position, the [0].

How do I loop all of results positions in order to extract the path from all of them?

For better understanding, please check the image and the code below: 结构体

The code so far looks like:

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);
});

Thanks in advance for any suggestions!

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('/')
        )
      );
    }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM