简体   繁体   English

Javascript - 动态推送到新数组?

[英]Javascript - Push to a new array dynamically?

Hello I am new to javascript so I'm sorry in advance if my explanation is not the best.您好,我是 javascript 的新手,所以如果我的解释不是最好的,我很抱歉。

I am getting my data back from an array called myData .我正在从一个名为myData的数组中取回我的数据。 I have a condition statement which checks the page url and depending on the url I am pushing a specific index of an array to a new array called stateArray我有一个条件语句检查页面 url 并取决于 url 我将数组的特定索引推送到一个名为stateArray的新数组

At the moment I am using the push method like this:目前我正在使用这样的推送方法:

stateArray.push(myData[1][5], myData[2][5], myData[3][5], myData[4][5], myData[5][5], myData[6][5], myData[7][5], myData[8][5], myData[9][5], myData[10][5], myData[11][5], myData[12][5], myData[13][5], myData[14][5], myData[15][5], myData[16][5], myData[17][5], myData][5])

the return of stateArray is giving back the data I am expecting but I am going to have ten different conditions and would like to know if there is a way of doing the push 17 times for every condition better? stateArray的返回正在返回我期望的数据,但我将有十个不同的条件,并且想知道是否有一种方法可以更好地为每个条件执行 17 次推送?

Every element is the same for the condition.对于条件,每个元素都是相同的。 For example例如

if (url.includes('/states/') {
stateArray.push(myData[1][5], myData[2][5], myData[3][5], myData[4][5], myData[5][5], myData[6][5], myData[7][5], myData[8][5], myData[9][5], myData[10][5], myData[11][5], myData[12][5], myData[13][5], myData[14][5], myData[15][5], myData[16][5], myData[17][5], myData][5])
} else if (url.includes('/homes/) {
stateArray.push(myData[1][6], myData[2][6], myData[3][6], myData[4][6], myData[5][6], myData[6][6], myData[7][6], myData[8][6], myData[9][6], myData[10][6], myData[11][6], myData[12][6], myData[13][6], myData[14][6], myData[15][6], myData[16][6], myData[17][6], myData][6])
} else if (url.incldues('/retail/) {
stateArray.push(myData[1][7], myData[2][7], myData[3][7], myData[4][7], myData[5][7], myData[6][7], myData[7][7], myData[8][7], myData[9][7], myData[10][7], myData[11][7], myData[12][7], myData[13][7], myData[14][7], myData[15][7], myData[16][7], myData[17][5], myData][7])

}

Like I mentioned earlier I currently have 10 conditions and it is very difficult to maintain and update.就像我之前提到的,我目前有 10 个条件,并且很难维护和更新。 Is there a way of generating the same results dynamically?有没有办法动态生成相同的结果? I believe this can be done through a loop but I am not familiar with the syntax in regards to pushing at a specific index and ending at a specific index.我相信这可以通过循环来完成,但我不熟悉在特定索引处推送和在特定索引处结束的语法。

My expected outcome is a short handed way of going through each condition and pushing into the new Array.我的预期结果是通过每个条件并推入新阵列的人手不足的方法。

You can use forEach :您可以使用forEach

if (url.includes('/states/')) {
  myData.forEach(e => stateArray.push(e[5]));
} else if (url.includes('/homes/')) {
  myData.forEach(e => stateArray.push(e[6]));
} else if (url.includes('/retail/')) {
  myData.forEach(e => stateArray.push(e[7]));
}

(Also note you need a second ) at the end of your if to close off both the if and the includes ) (另请注意,您需要第二个)if结束时关闭ifincludes

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

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