【问题标题】:Creating and pushing elements in an new array depends on conditions在新数组中创建和推送元素取决于条件
【发布时间】:2020-06-03 07:12:47
【问题描述】:

我的数据数组

data:[
0:{ 
   id:1 ,.....
   competetion:[ 
          0:{
              id: 1....,
              match:[ 
                      0:{id: 1 ......}, 
                      1:{same}
                      .
                      .
                    ]
          1:{same}]},
          2:{same}
          .
          .
        ]
  },
1:{same},
2:{same}
.
.
.
] 

对于data[],我可以创建一个带有推送元素的新数组(sportarr[]),但我想在同一个数组sportarr[] 中创建与competetion[]match[] 相同的数组 如果有任何其他方法可以做到这一点,请帮助我...

我的代码:我正在循环它:

this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)
const arrlength = response.data.length; 
for (let i = 0; i < arrlength; i++) {
    this.sportarr.push({       // I declared a new array where i want to push the element
      id: i,
      value: false
    });
}

【问题讨论】:

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


    【解决方案1】:

    如果您想要基于映射的自己的内容,我建议首先遍历数组,然后映射每个匹配和竞争,并在映射中编写您自己的逻辑

     const arrlength = data.length;
          for (let i = 0; i < arrlength; i++) {
            let competition = [];
            competition = data[i].competetion.map( (val, index) => {
              #write your own logic to produce the required outcome
              return {
                  id: index,
                  value: false
              };
            });
            this.sportarr.push({
              id: i,
              value: false,
              competetion: competition
            });
            console.log('myarrr...', this.sportarr);
    

    【讨论】:

      【解决方案2】:

      我感谢 Pathikrit Sanyal 和 Fahd Lihidheb 对于分析者 这是我根据 Pathikrit Sanyal 所做的更改

        for (let i = 0; i < arrlength; i++) {
              let competition = [];
              competition = response.data[i].competetion.map((val, index) => {
                let match = [];
                match = val.match.map((value, indx) => {
                  return {
                    id: indx,
                    value: false
                  };
                });
                return {
                  id: index,
                  value: false,
                  match
                };
              });
              this.sportarr.push({
                id: i,
                value: false,
                competetion: competition
              });
              console.log('myarrr...', this.sportarr);
            }
      

      现在我得到了我想要的

      【讨论】:

        【解决方案3】:

        我不确定您是否尝试为每个嵌套数组(竞争和匹配)创建一个数组,但您可以这样做

        this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)
        
        
        const competetionList = // Array of competetions
            [].concat.apply([], this.sportsSidebar.map(item => item.competetion));
        
        const matchList = // Array of matchs
            [].concat.apply([], competetionList .map(item => item.match));
        }
        

        【讨论】:

          猜你喜欢
          • 2022-01-02
          • 2014-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          • 2022-06-15
          相关资源
          最近更新 更多