打字稿如何搜索二维数组

时间:2019-01-03 23:40:23

标签: arrays angular typescript for-loop multidimensional-array

我正在研究2D数组,我想知道是否有一种更快的方法搜索数组而不使用嵌套的for循环。

以下是我目前所拥有的:

    dataArray: Array<Array<details>> = [
      [{result: true, context: {info: "stuff"}}, {result: false, context: {info: "more stuff"}}, ...],
      [{result: false, context: {info: "hi"}}, {result: true, context: {info: "more stuff"}}, ...],
      [{result: false, context: {info: "bye"}}, {result: false, context: {info: "more stuff"}}, ...],
      [{result: true, context: {info: "none"}}, {result: true, context: {info: "more stuff"}}, ...],
    ];

     for (let i = 0; i < this.dataArray.length; i++) {
       for (let j = 0; j < this.dataArray[i].length; j++) {
         if (this.dataArray[i][j].context.info === "hi") {
           console.log("item located in array");
        }
       }
      } 

是否有一种更快的方法来搜索这些数据,还是应该只使用double for循环?

0 个答案:

没有答案