merge array data with array data but only certain number like from array 1 – 5 assign a then 6-9 assign b

huangapple go评论65阅读模式
英文:

merge array data with array data but only certain number like from array 1 - 5 assign a then 6-9 assign b

问题

下午好
我正在尝试通过为一些元素分配一些值来合并两个对象,然后为另一些元素分配另一些值。我设法创建了一些东西,但它将一些东西放在了错误的位置,而且代码重复。以下是我的代码:

const data = [
  [
      "Greece"
  ],
  [
      "Denmark"
  ],
  // 其他国家的数据
];

const preset = [
  {
    "zone": 5821,
    "count": 36
  },
  {
    "zone": 5822,
    "count": 12
  },
  // 其他预设数据
];

const myArr = [];

for (let i = 0; i < preset.length; i++) {
  for (let t = 0; t < preset[i].count; t++) {
    myArr.push({
      zone: preset[i].zone,
      country: data[t][0],
      number: i + 1
    });
  }
}

它生成以下结果:

[ { zone: 5821, country: 'Greece', number: 1 },
  { zone: 5821, country: 'Denmark', number: 1 },
  // 其他结果数据
]
英文:

Good afternoon
I'm trying to merge 2 object by assigning some values to a number of element the other number to another number of elements. I managed to create some thing but it places some on the wrong position an the code is repetitive.
Here is my code

const data = [
[
&quot;Greece&quot;
],
[
&quot;Denmark&quot;
],
[
&quot;Finland&quot;
],
[
&quot;Norway&quot;
],
[
&quot;Sweden&quot;
],
[
&quot;Portugal&quot;
],
[
&quot;Austria&quot;
],
[
&quot;Belgium&quot;
],
[
&quot;Bulgaria&quot;
],
[
&quot;Croatia&quot;
],
[
&quot;Cyprus&quot;
],
[
&quot;Czech Republic&quot;
],
[
&quot;Estonia&quot;
],
[
&quot;France&quot;
],
[
&quot;Germany (Federal Republic of)&quot;
],
[
&quot;Gibraltar&quot;
],
[
&quot;Hungary&quot;
],
[
&quot;Iceland&quot;
],
[
&quot;Ireland&quot;
],
[
&quot;Italy&quot;
],
[
&quot;Latvia&quot;
],
[
&quot;Liechtenstein&quot;
],
[
&quot;Lithuania&quot;
],
[
&quot;Luxembourg&quot;
],
[
&quot;Malta&quot;
],
[
&quot;Netherlands&quot;
],
[
&quot;Poland&quot;
],
[
&quot;Reunion&quot;
],
[
&quot;Romania&quot;
],
[
&quot;Saint Pierre and Miquelon (Collectivit&#233; territoriale de la R&#233;publique fran&#231;aise)&quot;
],
[
&quot;Slovak Republic&quot;
],
[
&quot;Slovenia&quot;
],
[
&quot;Spain&quot;
],
[
&quot;Switzerland&quot;
],
[
&quot;United Kingdom of Great Britain and Northern Ireland&quot;
],
[
&quot;Kazakhstan&quot;
],
[
&quot;Turkey&quot;
],
[
&quot;Bosnia and Herzegovina&quot;
],
[
&quot;Faroe Islands&quot;
],
[
&quot;Israel&quot;
],
[
&quot;Macau&quot;
],
[
&quot;Azerbaijan&quot;
],
[
&quot;Guadeloupe&quot;
],
[
&quot;Fiji&quot;
],
[
&quot;Samoa&quot;
],
[
&quot;Tonga&quot;
],
[
&quot;Canada&quot;
],
[
&quot;United States of America&quot;
],
[
&quot;Monaco&quot;
],
[
&quot;Bermuda&quot;
],
[
&quot;Costa Rica&quot;
],
[
&quot;Guatemala&quot;
],
[
&quot;Guyana&quot;
],
[
&quot;Honduras&quot;
],
[
&quot;Papua New Guinea&quot;
],
[
&quot;Paraguay&quot;
],
[
&quot;Albania&quot;
],
[
&quot;Cambodia&quot;
],
[
&quot;Sri Lanka&quot;
],
[
&quot;Dominican Republic&quot;
],
[
&quot;Ecuador&quot;
],
[
&quot;Mongolia&quot;
],
[
&quot;Nicaragua&quot;
],
[
&quot;Vietnam&quot;
],
[
&quot;Jordan&quot;
],
[
&quot;Namibia&quot;
],
[
&quot;Afghanistan&quot;
],
[
&quot;Argentina&quot;
],
[
&quot;Botswana&quot;
],
[
&quot;Cameroon&quot;
],
[
&quot;Congo&quot;
],
[
&quot;Cote d&#39;Ivoire&quot;
],
[
&quot;Falkland Islands (Malvinas)&quot;
],
[
&quot;Ghana&quot;
],
[
&quot;Guinea&quot;
],
[
&quot;Guinea-Bissau&quot;
],
[
&quot;Kosovo&quot;
],
[
&quot;Liberia&quot;
],
[
&quot;Madagascar&quot;
],
[
&quot;Moldova&quot;
],
[
&quot;Nigeria&quot;
],
[
&quot;Republic of Rwanda&quot;
],
[
&quot;Senegal&quot;
],
[
&quot;Somalia&quot;
],
[
&quot;South Africa&quot;
],
[
&quot;Swaziland&quot;
],
[
&quot;Syrian Arab Republic&quot;
],
[
&quot;Uganda&quot;
],
[
&quot;Zambia&quot;
],
[
&quot;Mexico&quot;
],
[
&quot;Andorra&quot;
],
[
&quot;Belarus&quot;
],
[
&quot;Greenland (Denmark)&quot;
],
[
&quot;Montenegro&quot;
],
[
&quot;Serbia&quot;
],
[
&quot;Sierra Leone&quot;
],
[
&quot;The Former Yugoslav Republic of Macedonia&quot;
],
[
&quot;Ukraine&quot;
],
[
&quot;Russian Federation&quot;
],
[
&quot;Burkina Faso&quot;
],
[
&quot;Central African Republic&quot;
],
[
&quot;Niger&quot;
],
[
&quot;Democratic Republic of Congo&quot;
],
[
&quot;Armenia&quot;
],
[
&quot;Philippines&quot;
],
[
&quot;China&quot;
],
[
&quot;Gabon&quot;
],
[
&quot;Malawi&quot;
],
[
&quot;Japan&quot;
],
[
&quot;Kenya&quot;
],
[
&quot;Kyrgyzstan&quot;
],
[
&quot;Panama&quot;
],
[
&quot;Bangladesh&quot;
],
[
&quot;Georgia&quot;
],
[
&quot;Pakistan&quot;
],
[
&quot;Singapore&quot;
],
[
&quot;Chad&quot;
],
[
&quot;Indonesia&quot;
],
[
&quot;El Salvador&quot;
],
[
&quot;Algeria&quot;
],
[
&quot;Bahrain&quot;
],
[
&quot;Benin&quot;
],
[
&quot;Brunei Darussalam&quot;
],
[
&quot;Iran&quot;
],
[
&quot;Laos&quot;
],
[
&quot;Malaysia&quot;
],
[
&quot;Mali&quot;
],
[
&quot;Togo&quot;
],
[
&quot;Uzbekistan&quot;
],
[
&quot;Tanzania&quot;
],
[
&quot;Thailand&quot;
],
[
&quot;Mauritius&quot;
],
[
&quot;Anguilla&quot;
],
[
&quot;Antigua and Barbuda&quot;
],
[
&quot;Barbados&quot;
],
[
&quot;British Virgin Islands&quot;
],
[
&quot;Cayman Islands&quot;
],
[
&quot;Dominica&quot;
],
[
&quot;Grenada&quot;
],
[
&quot;Jamaica&quot;
],
[
&quot;Montserrat&quot;
],
[
&quot;Saint Kitts and Nevis&quot;
],
[
&quot;Saint Lucia&quot;
],
[
&quot;Saint Vincent and the Grenadines&quot;
],
[
&quot;Virgin Islands&quot;
],
[
&quot;Tunisia&quot;
],
[
&quot;Chile&quot;
],
[
&quot;Australia&quot;
],
[
&quot;Cabo Verde&quot;
],
[
&quot;Cura&#231;ao&quot;
],
[
&quot;Myanmar&quot;
],
[
&quot;Gambia&quot;
],
[
&quot;Brazil&quot;
],
[
&quot;Suriname&quot;
],
[
&quot;Trinidad and Tobago&quot;
],
[
&quot;Qatar&quot;
],
[
&quot;Seychelles&quot;
],
[
&quot;Burundi&quot;
],
[
&quot;French Polynesia&quot;
],
[
&quot;Haiti&quot;
],
[
&quot;Bolivia&quot;
],
[
&quot;Belize&quot;
],
[
&quot;Hong Kong&quot;
],
[
&quot;Taiwan&quot;
],
[
&quot;Kuwait&quot;
],
[
&quot;Zimbabwe&quot;
],
[
&quot;Nepal&quot;
],
[
&quot;New Zealand&quot;
],
[
&quot;Egypt&quot;
],
[
&quot;Angola&quot;
],
[
&quot;Lesotho&quot;
],
[
&quot;Mozambique&quot;
],
[
&quot;Puerto Rico&quot;
],
[
&quot;Peru&quot;
],
[
&quot;Equatorial Guinea&quot;
],
[
&quot;Uruguay&quot;
],
[
&quot;Venezuela&quot;
],
[
&quot;India&quot;
],
[
&quot;Maldives&quot;
],
[
&quot;Republic of Korea&quot;
],
[
&quot;Turkmenistan&quot;
],
[
&quot;Tajikistan&quot;
],
[
&quot;Cuba&quot;
],
[
&quot;Saudi Arabia&quot;
],
[
&quot;Timor-Leste&quot;
],
[
&quot;Iraq&quot;
],
[
&quot;Libya&quot;
],
[
&quot;Mauritania&quot;
],
[
&quot;Morocco&quot;
],
[
&quot;Republic of Djibouti&quot;
],
[
&quot;Sudan&quot;
],
[
&quot;Lebanon&quot;
],
[
&quot;Oman&quot;
],
[
&quot;United Arab Emirates&quot;
],
[
&quot;Yemen&quot;
]
];
const preset = [
{
&quot;zone&quot;: 5821,
&quot;count&quot;: 36
},
{
&quot;zone&quot;: 5822,
&quot;count&quot;: 12
},
{
&quot;zone&quot;: 5823,
&quot;count&quot;: 53
},
{
&quot;zone&quot;: 5824,
&quot;count&quot;: 28
},
{
&quot;zone&quot;: 5825,
&quot;count&quot;: 27
},
{
&quot;zone&quot;: 5826,
&quot;count&quot;: 16
},
{
&quot;zone&quot;: 5827,
&quot;count&quot;: 23
}
];
const myArr = [];
for (let i = 0; i &lt; preset.length; i++) {
for (let t = 0; t &lt; preset[i].count; t++) {
myArr.push({
zone: preset[i].zone,
country: data[t][0],
number: i + 1
});
}
}

It produces the following results.

[ { zone: 5821, country: &#39;Greece&#39;, number: 1 },
  { zone: 5821, country: &#39;Denmark&#39;, number: 1 },
  { zone: 5821, country: &#39;Finland&#39;, number: 1 },
  { zone: 5821, country: &#39;Norway&#39;, number: 1 },
  { zone: 5821, country: &#39;Sweden&#39;, number: 1 },
  { zone: 5821, country: &#39;Portugal&#39;, number: 1 },
  { zone: 5821, country: &#39;Austria&#39;, number: 1 },
  { zone: 5821, country: &#39;Belgium&#39;, number: 1 },
  { zone: 5821, country: &#39;Bulgaria&#39;, number: 1 },
  { zone: 5821, country: &#39;Croatia&#39;, number: 1 },
  { zone: 5821, country: &#39;Cyprus&#39;, number: 1 },
  { zone: 5821, country: &#39;Czech Republic&#39;, number: 1 },
  { zone: 5821, country: &#39;Estonia&#39;, number: 1 },
  { zone: 5821, country: &#39;France&#39;, number: 1 },
  { zone: 5821,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 1 },
  { zone: 5821, country: &#39;Gibraltar&#39;, number: 1 },
  { zone: 5821, country: &#39;Hungary&#39;, number: 1 },
  { zone: 5821, country: &#39;Iceland&#39;, number: 1 },
  { zone: 5821, country: &#39;Ireland&#39;, number: 1 },
  { zone: 5821, country: &#39;Italy&#39;, number: 1 },
  { zone: 5821, country: &#39;Latvia&#39;, number: 1 },
  { zone: 5821, country: &#39;Liechtenstein&#39;, number: 1 },
  { zone: 5821, country: &#39;Lithuania&#39;, number: 1 },
  { zone: 5821, country: &#39;Luxembourg&#39;, number: 1 },
  { zone: 5821, country: &#39;Malta&#39;, number: 1 },
  { zone: 5821, country: &#39;Netherlands&#39;, number: 1 },
  { zone: 5821, country: &#39;Poland&#39;, number: 1 },
  { zone: 5821, country: &#39;Reunion&#39;, number: 1 },
  { zone: 5821, country: &#39;Romania&#39;, number: 1 },
  { zone: 5821,
    country: &#39;Saint Pierre and Miquelon (Collectivit&#233; territoriale de la R&#233;publique fran&#231;aise)&#39;,
    number: 1 },
  { zone: 5821, country: &#39;Slovak Republic&#39;, number: 1 },
  { zone: 5821, country: &#39;Slovenia&#39;, number: 1 },
  { zone: 5821, country: &#39;Spain&#39;, number: 1 },
  { zone: 5821, country: &#39;Switzerland&#39;, number: 1 },
  { zone: 5821,
    country: &#39;United Kingdom of Great Britain and Northern Ireland&#39;,
    number: 1 },
  { zone: 5821, country: &#39;Kazakhstan&#39;, number: 1 },
  { zone: 5822, country: &#39;Greece&#39;, number: 2 },
  { zone: 5822, country: &#39;Denmark&#39;, number: 2 },
  { zone: 5822, country: &#39;Finland&#39;, number: 2 },
  { zone: 5822, country: &#39;Norway&#39;, number: 2 },
  { zone: 5822, country: &#39;Sweden&#39;, number: 2 },
  { zone: 5822, country: &#39;Portugal&#39;, number: 2 },
  { zone: 5822, country: &#39;Austria&#39;, number: 2 },
  { zone: 5822, country: &#39;Belgium&#39;, number: 2 },
  { zone: 5822, country: &#39;Bulgaria&#39;, number: 2 },
  { zone: 5822, country: &#39;Croatia&#39;, number: 2 },
  { zone: 5822, country: &#39;Cyprus&#39;, number: 2 },
  { zone: 5822, country: &#39;Czech Republic&#39;, number: 2 },
  { zone: 5823, country: &#39;Greece&#39;, number: 3 },
  { zone: 5823, country: &#39;Denmark&#39;, number: 3 },
  { zone: 5823, country: &#39;Finland&#39;, number: 3 },
  { zone: 5823, country: &#39;Norway&#39;, number: 3 },
  { zone: 5823, country: &#39;Sweden&#39;, number: 3 },
  { zone: 5823, country: &#39;Portugal&#39;, number: 3 },
  { zone: 5823, country: &#39;Austria&#39;, number: 3 },
  { zone: 5823, country: &#39;Belgium&#39;, number: 3 },
  { zone: 5823, country: &#39;Bulgaria&#39;, number: 3 },
  { zone: 5823, country: &#39;Croatia&#39;, number: 3 },
  { zone: 5823, country: &#39;Cyprus&#39;, number: 3 },
  { zone: 5823, country: &#39;Czech Republic&#39;, number: 3 },
  { zone: 5823, country: &#39;Estonia&#39;, number: 3 },
  { zone: 5823, country: &#39;France&#39;, number: 3 },
  { zone: 5823,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 3 },
  { zone: 5823, country: &#39;Gibraltar&#39;, number: 3 },
  { zone: 5823, country: &#39;Hungary&#39;, number: 3 },
  { zone: 5823, country: &#39;Iceland&#39;, number: 3 },
  { zone: 5823, country: &#39;Ireland&#39;, number: 3 },
  { zone: 5823, country: &#39;Italy&#39;, number: 3 },
  { zone: 5823, country: &#39;Latvia&#39;, number: 3 },
  { zone: 5823, country: &#39;Liechtenstein&#39;, number: 3 },
  { zone: 5823, country: &#39;Lithuania&#39;, number: 3 },
  { zone: 5823, country: &#39;Luxembourg&#39;, number: 3 },
  { zone: 5823, country: &#39;Malta&#39;, number: 3 },
  { zone: 5823, country: &#39;Netherlands&#39;, number: 3 },
  { zone: 5823, country: &#39;Poland&#39;, number: 3 },
  { zone: 5823, country: &#39;Reunion&#39;, number: 3 },
  { zone: 5823, country: &#39;Romania&#39;, number: 3 },
  { zone: 5823,
    country: &#39;Saint Pierre and Miquelon (Collectivit&#233; territoriale de la R&#233;publique fran&#231;aise)&#39;,
    number: 3 },
  { zone: 5823, country: &#39;Slovak Republic&#39;, number: 3 },
  { zone: 5823, country: &#39;Slovenia&#39;, number: 3 },
  { zone: 5823, country: &#39;Spain&#39;, number: 3 },
  { zone: 5823, country: &#39;Switzerland&#39;, number: 3 },
  { zone: 5823,
    country: &#39;United Kingdom of Great Britain and Northern Ireland&#39;,
    number: 3 },
  { zone: 5823, country: &#39;Kazakhstan&#39;, number: 3 },
  { zone: 5823, country: &#39;Turkey&#39;, number: 3 },
  { zone: 5823, country: &#39;Bosnia and Herzegovina&#39;, number: 3 },
  { zone: 5823, country: &#39;Faroe Islands&#39;, number: 3 },
  { zone: 5823, country: &#39;Israel&#39;, number: 3 },
  { zone: 5823, country: &#39;Macau&#39;, number: 3 },
  { zone: 5823, country: &#39;Azerbaijan&#39;, number: 3 },
  { zone: 5823, country: &#39;Guadeloupe&#39;, number: 3 },
  { zone: 5823, country: &#39;Fiji&#39;, number: 3 },
  { zone: 5823, country: &#39;Samoa&#39;, number: 3 },
  { zone: 5823, country: &#39;Tonga&#39;, number: 3 },
  { zone: 5823, country: &#39;Canada&#39;, number: 3 },
  { zone: 5823, country: &#39;United States of America&#39;, number: 3 },
  { zone: 5823, country: &#39;Monaco&#39;, number: 3 },
  { zone: 5823, country: &#39;Bermuda&#39;, number: 3 },
  { zone: 5823, country: &#39;Costa Rica&#39;, number: 3 },
  { zone: 5823, country: &#39;Guatemala&#39;, number: 3 },
  { zone: 5823, country: &#39;Guyana&#39;, number: 3 },
  { zone: 5824, country: &#39;Greece&#39;, number: 4 },
  { zone: 5824, country: &#39;Denmark&#39;, number: 4 },
  { zone: 5824, country: &#39;Finland&#39;, number: 4 },
  { zone: 5824, country: &#39;Norway&#39;, number: 4 },
  { zone: 5824, country: &#39;Sweden&#39;, number: 4 },
  { zone: 5824, country: &#39;Portugal&#39;, number: 4 },
  { zone: 5824, country: &#39;Austria&#39;, number: 4 },
  { zone: 5824, country: &#39;Belgium&#39;, number: 4 },
  { zone: 5824, country: &#39;Bulgaria&#39;, number: 4 },
  { zone: 5824, country: &#39;Croatia&#39;, number: 4 },
  { zone: 5824, country: &#39;Cyprus&#39;, number: 4 },
  { zone: 5824, country: &#39;Czech Republic&#39;, number: 4 },
  { zone: 5824, country: &#39;Estonia&#39;, number: 4 },
  { zone: 5824, country: &#39;France&#39;, number: 4 },
  { zone: 5824,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 4 },
  { zone: 5824, country: &#39;Gibraltar&#39;, number: 4 },
  { zone: 5824, country: &#39;Hungary&#39;, number: 4 },
  { zone: 5824, country: &#39;Iceland&#39;, number: 4 },
  { zone: 5824, country: &#39;Ireland&#39;, number: 4 },
  { zone: 5824, country: &#39;Italy&#39;, number: 4 },
  { zone: 5824, country: &#39;Latvia&#39;, number: 4 },
  { zone: 5824, country: &#39;Liechtenstein&#39;, number: 4 },
  { zone: 5824, country: &#39;Lithuania&#39;, number: 4 },
  { zone: 5824, country: &#39;Luxembourg&#39;, number: 4 },
  { zone: 5824, country: &#39;Malta&#39;, number: 4 },
  { zone: 5824, country: &#39;Netherlands&#39;, number: 4 },
  { zone: 5824, country: &#39;Poland&#39;, number: 4 },
  { zone: 5824, country: &#39;Reunion&#39;, number: 4 },
  { zone: 5825, country: &#39;Greece&#39;, number: 5 },
  { zone: 5825, country: &#39;Denmark&#39;, number: 5 },
  { zone: 5825, country: &#39;Finland&#39;, number: 5 },
  { zone: 5825, country: &#39;Norway&#39;, number: 5 },
  { zone: 5825, country: &#39;Sweden&#39;, number: 5 },
  { zone: 5825, country: &#39;Portugal&#39;, number: 5 },
  { zone: 5825, country: &#39;Austria&#39;, number: 5 },
  { zone: 5825, country: &#39;Belgium&#39;, number: 5 },
  { zone: 5825, country: &#39;Bulgaria&#39;, number: 5 },
  { zone: 5825, country: &#39;Croatia&#39;, number: 5 },
  { zone: 5825, country: &#39;Cyprus&#39;, number: 5 },
  { zone: 5825, country: &#39;Czech Republic&#39;, number: 5 },
  { zone: 5825, country: &#39;Estonia&#39;, number: 5 },
  { zone: 5825, country: &#39;France&#39;, number: 5 },
  { zone: 5825,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 5 },
  { zone: 5825, country: &#39;Gibraltar&#39;, number: 5 },
  { zone: 5825, country: &#39;Hungary&#39;, number: 5 },
  { zone: 5825, country: &#39;Iceland&#39;, number: 5 },
  { zone: 5825, country: &#39;Ireland&#39;, number: 5 },
  { zone: 5825, country: &#39;Italy&#39;, number: 5 },
  { zone: 5825, country: &#39;Latvia&#39;, number: 5 },
  { zone: 5825, country: &#39;Liechtenstein&#39;, number: 5 },
  { zone: 5825, country: &#39;Lithuania&#39;, number: 5 },
  { zone: 5825, country: &#39;Luxembourg&#39;, number: 5 },
  { zone: 5825, country: &#39;Malta&#39;, number: 5 },
  { zone: 5825, country: &#39;Netherlands&#39;, number: 5 },
  { zone: 5825, country: &#39;Poland&#39;, number: 5 },
  { zone: 5826, country: &#39;Greece&#39;, number: 6 },
  { zone: 5826, country: &#39;Denmark&#39;, number: 6 },
  { zone: 5826, country: &#39;Finland&#39;, number: 6 },
  { zone: 5826, country: &#39;Norway&#39;, number: 6 },
  { zone: 5826, country: &#39;Sweden&#39;, number: 6 },
  { zone: 5826, country: &#39;Portugal&#39;, number: 6 },
  { zone: 5826, country: &#39;Austria&#39;, number: 6 },
  { zone: 5826, country: &#39;Belgium&#39;, number: 6 },
  { zone: 5826, country: &#39;Bulgaria&#39;, number: 6 },
  { zone: 5826, country: &#39;Croatia&#39;, number: 6 },
  { zone: 5826, country: &#39;Cyprus&#39;, number: 6 },
  { zone: 5826, country: &#39;Czech Republic&#39;, number: 6 },
  { zone: 5826, country: &#39;Estonia&#39;, number: 6 },
  { zone: 5826, country: &#39;France&#39;, number: 6 },
  { zone: 5826,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 6 },
  { zone: 5826, country: &#39;Gibraltar&#39;, number: 6 },
  { zone: 5827, country: &#39;Greece&#39;, number: 7 },
  { zone: 5827, country: &#39;Denmark&#39;, number: 7 },
  { zone: 5827, country: &#39;Finland&#39;, number: 7 },
  { zone: 5827, country: &#39;Norway&#39;, number: 7 },
  { zone: 5827, country: &#39;Sweden&#39;, number: 7 },
  { zone: 5827, country: &#39;Portugal&#39;, number: 7 },
  { zone: 5827, country: &#39;Austria&#39;, number: 7 },
  { zone: 5827, country: &#39;Belgium&#39;, number: 7 },
  { zone: 5827, country: &#39;Bulgaria&#39;, number: 7 },
  { zone: 5827, country: &#39;Croatia&#39;, number: 7 },
  { zone: 5827, country: &#39;Cyprus&#39;, number: 7 },
  { zone: 5827, country: &#39;Czech Republic&#39;, number: 7 },
  { zone: 5827, country: &#39;Estonia&#39;, number: 7 },
  { zone: 5827, country: &#39;France&#39;, number: 7 },
  { zone: 5827,
    country: &#39;Germany (Federal Republic of)&#39;,
    number: 7 },
  { zone: 5827, country: &#39;Gibraltar&#39;, number: 7 },
  { zone: 5827, country: &#39;Hungary&#39;, number: 7 },
  { zone: 5827, country: &#39;Iceland&#39;, number: 7 },
  { zone: 5827, country: &#39;Ireland&#39;, number: 7 },
  { zone: 5827, country: &#39;Italy&#39;, number: 7 },
  { zone: 5827, country: &#39;Latvia&#39;, number: 7 },
  { zone: 5827, country: &#39;Liechtenstein&#39;, number: 7 },
  { zone: 5827, country: &#39;Lithuania&#39;, number: 7 } ]

As you can see zone: 5820 is assigned to Iraq but it is not supposed to do that. I would like some help.

Expected results each zone is supposed to be assigned to with countries from index 0 to 35 which is the count element in the preset but It repeats and I see the issue I just can't figure it out.
My plan was for each zone loop count times and assign countries but I didn't foresee that it will start from index 0 every count

Thanks in advance

答案1

得分: 1

你可以使用 Array#flatMap 并将当前索引存储在闭包中。

const data = [["希腊"],["丹麦"],["芬兰"],["挪威"],["瑞典"],["葡萄牙"],["奥地利"],["比利时"],["保加利亚"],["克罗地亚"],["塞浦路斯"],["捷克共和国"],["爱沙尼亚"],["法国"],["德国(联邦共和国)"],["直布罗陀"],["匈牙利"],["冰岛"],["爱尔兰"],["意大利"],["拉脱维亚"],["列支敦士登"],["立陶宛"],["卢森堡"],["马耳他"],["荷兰"],["波兰"],["留尼汪"],["罗马尼亚"],["圣皮埃尔和密克隆(法兰西共和国领土)"],["斯洛伐克共和国"],["斯洛文尼亚"],["西班牙"],["瑞士"],["大不列颠和北爱尔兰联合王国"],["哈萨克斯坦"],["土耳其"],["波斯尼亚和黑塞哥维那"],["法罗群岛"],["以色列"],["澳门"],["阿塞拜疆"],["瓜德罗普"],["斐济"],["萨摩亚"],["汤加"],["加拿大"],["美利坚合众国"],["摩纳哥"],["百慕大"],["哥斯达黎加"],["危地马拉"],["圭亚那"],["洪都拉斯"],["巴布亚新几内亚"],["巴拉圭"],["阿尔巴尼亚"],["柬埔寨"],["斯里兰卡"],["多米尼加共和国"],["厄瓜多尔"],["蒙古"],["尼加拉瓜"],["越南"],["约旦"],["纳米比亚"],["阿富汗"],["阿根廷"],["博茨瓦纳"],["喀麦隆"],["刚果"],["科特迪瓦"],["福克兰群岛(马尔维纳斯群岛)"],["加纳"],["几内亚"],["几内亚比绍"],["科索沃"],["利比里亚"],["马达加斯加"],["摩尔多瓦"],["尼日利亚"],["卢旺达共和国"],["塞内加尔"],["索马里"],["南非"],["斯威士兰"],["叙利亚阿拉伯共和国"],["乌干达"],["赞比亚"],["墨西哥"],["安道尔"],["白俄罗斯"],["格陵兰(丹麦)"],["黑山"],["塞尔维亚"],["塞拉利昂"],["马其顿前南斯拉夫共和国"],["乌克兰"],["俄罗斯联邦"],["布基纳法索"],["中非共和国"],["尼日尔"],["刚果民主共和国"],["亚美尼亚"],["菲律宾"],["中国"],["加蓬"],["马拉维"],["日本"],["肯尼亚"],["吉尔吉斯斯坦"],["巴拿马"],["孟加拉国"],["格鲁吉亚"],["巴基斯坦"],["新加坡"],["乍得"],["印度尼西亚"],["萨尔瓦多"],["阿尔及利亚"],["巴林"],["贝宁"],["文莱达鲁萨兰国"],["伊朗"],["老挝"],["马来西亚"],["马里"],["多哥"],["乌兹别克斯坦"],["坦桑尼亚"],["泰国"],["毛里求斯"],["安圭拉"],["安提瓜和巴布达"],["巴巴多斯"],["英属维尔京群岛"],["开曼群岛"],["多米尼加"],["格林纳达"],["牙买加"],["蒙特塞拉特"],["圣基茨和尼维斯"],["圣卢西亚"],["圣文森特和格林纳丁斯"],["维尔京群岛"],["突尼斯"],["智利"],["澳大利亚"],["佛得角"],["库拉索"],["缅甸"],["冈比亚"],["巴西"],["苏里南"],["特立尼达和多巴哥"],["卡塔尔"],["塞舌尔"],["布隆迪"],["法属波利尼西亚"],["海地"],["玻利维亚"],["伯利兹"],["香港"],["台湾"],["科威特"],["津巴布韦"],["尼泊尔"],["新西兰"],["埃及"],["安哥拉"],["莱索托"],["莫桑比克"],["波多黎各"],["秘鲁"],["赤道几内亚"],["乌拉圭"],["委内瑞拉"],["印度"],["马尔代夫"],["韩国共和国"],["土库曼斯坦"],["塔吉克斯坦"],["古巴"],["沙特阿拉伯"],["东帝汶"],["伊拉克"],["利比亚"],["毛里塔尼亚"],["摩洛哥"],["吉布提共和国"],["苏丹"],["黎巴嫩"],["阿曼"],["阿拉伯联合酋长国"],["也门"]];
const res = preset.flatMap((c => (o, i) => data.slice(c, c += o.count).map(x => ({
    zone: o.zone,
    country: x[0],
    number: i + 1
})))(0));
console.log(res);

这是你提供的代码的

英文:

You can use Array#flatMap and store the current index in a closure.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data=[[&quot;Greece&quot;],[&quot;Denmark&quot;],[&quot;Finland&quot;],[&quot;Norway&quot;],[&quot;Sweden&quot;],[&quot;Portugal&quot;],[&quot;Austria&quot;],[&quot;Belgium&quot;],[&quot;Bulgaria&quot;],[&quot;Croatia&quot;],[&quot;Cyprus&quot;],[&quot;Czech Republic&quot;],[&quot;Estonia&quot;],[&quot;France&quot;],[&quot;Germany (Federal Republic of)&quot;],[&quot;Gibraltar&quot;],[&quot;Hungary&quot;],[&quot;Iceland&quot;],[&quot;Ireland&quot;],[&quot;Italy&quot;],[&quot;Latvia&quot;],[&quot;Liechtenstein&quot;],[&quot;Lithuania&quot;],[&quot;Luxembourg&quot;],[&quot;Malta&quot;],[&quot;Netherlands&quot;],[&quot;Poland&quot;],[&quot;Reunion&quot;],[&quot;Romania&quot;],[&quot;Saint Pierre and Miquelon (Collectivit\xe9 territoriale de la R\xe9publique fran\xe7aise)&quot;],[&quot;Slovak Republic&quot;],[&quot;Slovenia&quot;],[&quot;Spain&quot;],[&quot;Switzerland&quot;],[&quot;United Kingdom of Great Britain and Northern Ireland&quot;],[&quot;Kazakhstan&quot;],[&quot;Turkey&quot;],[&quot;Bosnia and Herzegovina&quot;],[&quot;Faroe Islands&quot;],[&quot;Israel&quot;],[&quot;Macau&quot;],[&quot;Azerbaijan&quot;],[&quot;Guadeloupe&quot;],[&quot;Fiji&quot;],[&quot;Samoa&quot;],[&quot;Tonga&quot;],[&quot;Canada&quot;],[&quot;United States of America&quot;],[&quot;Monaco&quot;],[&quot;Bermuda&quot;],[&quot;Costa Rica&quot;],[&quot;Guatemala&quot;],[&quot;Guyana&quot;],[&quot;Honduras&quot;],[&quot;Papua New Guinea&quot;],[&quot;Paraguay&quot;],[&quot;Albania&quot;],[&quot;Cambodia&quot;],[&quot;Sri Lanka&quot;],[&quot;Dominican Republic&quot;],[&quot;Ecuador&quot;],[&quot;Mongolia&quot;],[&quot;Nicaragua&quot;],[&quot;Vietnam&quot;],[&quot;Jordan&quot;],[&quot;Namibia&quot;],[&quot;Afghanistan&quot;],[&quot;Argentina&quot;],[&quot;Botswana&quot;],[&quot;Cameroon&quot;],[&quot;Congo&quot;],[&quot;Cote d&#39;Ivoire&quot;],[&quot;Falkland Islands (Malvinas)&quot;],[&quot;Ghana&quot;],[&quot;Guinea&quot;],[&quot;Guinea-Bissau&quot;],[&quot;Kosovo&quot;],[&quot;Liberia&quot;],[&quot;Madagascar&quot;],[&quot;Moldova&quot;],[&quot;Nigeria&quot;],[&quot;Republic of Rwanda&quot;],[&quot;Senegal&quot;],[&quot;Somalia&quot;],[&quot;South Africa&quot;],[&quot;Swaziland&quot;],[&quot;Syrian Arab Republic&quot;],[&quot;Uganda&quot;],[&quot;Zambia&quot;],[&quot;Mexico&quot;],[&quot;Andorra&quot;],[&quot;Belarus&quot;],[&quot;Greenland (Denmark)&quot;],[&quot;Montenegro&quot;],[&quot;Serbia&quot;],[&quot;Sierra Leone&quot;],[&quot;The Former Yugoslav Republic of Macedonia&quot;],[&quot;Ukraine&quot;],[&quot;Russian Federation&quot;],[&quot;Burkina Faso&quot;],[&quot;Central African Republic&quot;],[&quot;Niger&quot;],[&quot;Democratic Republic of Congo&quot;],[&quot;Armenia&quot;],[&quot;Philippines&quot;],[&quot;China&quot;],[&quot;Gabon&quot;],[&quot;Malawi&quot;],[&quot;Japan&quot;],[&quot;Kenya&quot;],[&quot;Kyrgyzstan&quot;],[&quot;Panama&quot;],[&quot;Bangladesh&quot;],[&quot;Georgia&quot;],[&quot;Pakistan&quot;],[&quot;Singapore&quot;],[&quot;Chad&quot;],[&quot;Indonesia&quot;],[&quot;El Salvador&quot;],[&quot;Algeria&quot;],[&quot;Bahrain&quot;],[&quot;Benin&quot;],[&quot;Brunei Darussalam&quot;],[&quot;Iran&quot;],[&quot;Laos&quot;],[&quot;Malaysia&quot;],[&quot;Mali&quot;],[&quot;Togo&quot;],[&quot;Uzbekistan&quot;],[&quot;Tanzania&quot;],[&quot;Thailand&quot;],[&quot;Mauritius&quot;],[&quot;Anguilla&quot;],[&quot;Antigua and Barbuda&quot;],[&quot;Barbados&quot;],[&quot;British Virgin Islands&quot;],[&quot;Cayman Islands&quot;],[&quot;Dominica&quot;],[&quot;Grenada&quot;],[&quot;Jamaica&quot;],[&quot;Montserrat&quot;],[&quot;Saint Kitts and Nevis&quot;],[&quot;Saint Lucia&quot;],[&quot;Saint Vincent and the Grenadines&quot;],[&quot;Virgin Islands&quot;],[&quot;Tunisia&quot;],[&quot;Chile&quot;],[&quot;Australia&quot;],[&quot;Cabo Verde&quot;],[&quot;Cura\xe7ao&quot;],[&quot;Myanmar&quot;],[&quot;Gambia&quot;],[&quot;Brazil&quot;],[&quot;Suriname&quot;],[&quot;Trinidad and Tobago&quot;],[&quot;Qatar&quot;],[&quot;Seychelles&quot;],[&quot;Burundi&quot;],[&quot;French Polynesia&quot;],[&quot;Haiti&quot;],[&quot;Bolivia&quot;],[&quot;Belize&quot;],[&quot;Hong Kong&quot;],[&quot;Taiwan&quot;],[&quot;Kuwait&quot;],[&quot;Zimbabwe&quot;],[&quot;Nepal&quot;],[&quot;New Zealand&quot;],[&quot;Egypt&quot;],[&quot;Angola&quot;],[&quot;Lesotho&quot;],[&quot;Mozambique&quot;],[&quot;Puerto Rico&quot;],[&quot;Peru&quot;],[&quot;Equatorial Guinea&quot;],[&quot;Uruguay&quot;],[&quot;Venezuela&quot;],[&quot;India&quot;],[&quot;Maldives&quot;],[&quot;Republic of Korea&quot;],[&quot;Turkmenistan&quot;],[&quot;Tajikistan&quot;],[&quot;Cuba&quot;],[&quot;Saudi Arabia&quot;],[&quot;Timor-Leste&quot;],[&quot;Iraq&quot;],[&quot;Libya&quot;],[&quot;Mauritania&quot;],[&quot;Morocco&quot;],[&quot;Republic of Djibouti&quot;],[&quot;Sudan&quot;],[&quot;Lebanon&quot;],[&quot;Oman&quot;],[&quot;United Arab Emirates&quot;],[&quot;Yemen&quot;]],preset=[{zone:5821,count:36},{zone:5822,count:12},{zone:5823,count:53},{zone:5824,count:28},{zone:5825,count:27},{zone:5826,count:16},{zone:5827,count:23}];
const res = preset.flatMap((c =&gt; (o, i) =&gt; data.slice(c, c += o.count).map(x =&gt; ({
zone: o.zone,
country: x[0],
number: i + 1
})))(0));
console.log(res);

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的部分:

从您的以下代码中

if (i <= 2) {
myArr.push({
  zone: preset[0].zone,
  country: data[i][0],
  number: 1
});
}

上面的代码将在i等于0、1和2时运行,即沙特、东帝汶和伊拉克。如果您希望所有区域仅适用于两个项目,请更改您的if逻辑如下

if (i <= 1) {
    myArr.push({
      zone: preset[0].zone,
      country: data[i][0],
      number: 1
    });
  } 
  else if (i > 1 && i <= 3) {
    myArr.push({
      zone: preset[1].zone,
      country: data[i][0],
      number: 2
    });
  }
  else if (i > 3 && i <= 5) {
    myArr.push({
      zone: preset[2].zone,
      country: data[i][0],
      number: 3
    });
  }
  else if (i > 5 && i <= 7) {
    myArr.push({
      zone: preset[3].zone,
      country: data[i][0],
      number: 4
    });
  }
  else if (i > 7 && i <= 9) {
    myArr.push({
      zone: preset[4].zone,
      country: data[i][0],
      number: 5
    });
  }
  else if (i > 9 && i <= 11) {
    myArr.push({
      zone: preset[5].zone,
      country: data[i][0],
      number: 6
    });
  }
  else {
    console.log("TEST");
  }

您还可以将整个过程简化为如下所示

index = Math.floor(i/2)
myArr.push({
  zone: preset[index].zone,
  country: data[i][0],
  number: index+1
});

请注意,这是您提供的代码的翻译版本,没有其他内容。

英文:

What is it supposed to do then?

From Your code below

if (**i &lt;= 2**) {
myArr.push({
zone: preset[0].zone,
country: data[i][0],
number: 1
});
}

Above will run for i = 0, 1 and 2, which is Saudi, Timor and Iraq. If you want all of the zones to only be applied to two items, then change your if logic to say below

if (i &lt;= 1) {
myArr.push({
zone: preset[0].zone,
country: data[i][0],
number: 1
});
} 
else if (i &gt; 1 &amp;&amp; i &lt;= 3) {
myArr.push({
zone: preset[1].zone,
country: data[i][0],
number: 2
});
}
else if (i &gt; 3 &amp;&amp; i &lt;= 5) {
myArr.push({
zone: preset[2].zone,
country: data[i][0],
number: 3
});
}
else if (i &gt; 5 &amp;&amp; i &lt;= 7) {
myArr.push({
zone: preset[3].zone,
country: data[i][0],
number: 4
});
}
else if (i &gt; 7 &amp;&amp; i &lt;= 9) {
myArr.push({
zone: preset[4].zone,
country: data[i][0],
number: 5
});
}
else if (i &gt; 9 &amp;&amp; i &lt;= 11) {
myArr.push({
zone: preset[5].zone,
country: data[i][0],
number: 6
});
}
else {
console.log(&quot;TEST&quot;);
}

You can also simplify the entire thing to say

index = Math.floor(i/2)
myArr.push({
zone: preset[index].zone,
country: data[i][0],
number: index+1
});

huangapple
  • 本文由 发表于 2023年7月13日 21:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定