简体   繁体   English

如何使用扩展运算符 Javascript 动态推送数组

[英]How to dynamically push an array using spread operator Javascript

[
  {
    "details": {
      "name": "john",
      "point": "20"
    },

    "list": {
      "number": "30",

    }
  },
  {
    "details": {
      "name": "doe",
      "point": "25"
    },
    "list": {
      "number": "30",

    }
  }
]

This is what i am trying to do, i am getting the data from the store and if the response is only one i use data = getData[0].details and if the response is more than one then i push the data using [...data, ...getData[1].details] if there are more than one data how can i achieve.这就是我想要做的,我从商店获取数据,如果响应只有一个,我使用 data = getData[0].details ,如果响应超过一个,那么我使用 [. ..data, ...getData[1].details] 如果有多个数据,我该如何实现。 thanks in advance提前致谢

 let data:any = [];
     this.store
              .pipe(
                select(getData),
                map((getData,i) => {
                  if (getData) {
                      data = [...data, ...getData[i].details]
                  }
             return data;
                })
             )

I think you want to get an array of the details.我认为您想获得一系列详细信息。 In this case you can say.在这种情况下,你可以说。

 let data = [ { "details": { "name": "john", "point": "20" }, "list": { "number": "30", } }, { "details": { "name": "doe", "point": "25" }, "list": { "number": "30", } } ]; let details = data.map(a => a.details); console.log(details);

I think i understand what you mean:我想我明白你的意思:

What I would do is map over your getData response and add to the original array on each iteration.我要做的是映射您的getData响应并在每次迭代时添加到原始数组中。 It wont matter if there is 1 or many in the getData array: getData 数组中有 1 个还是多个无关紧要:

getData.map(x => $data.push(x.details));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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