简体   繁体   English

如何使用 Object.entries() 方法 JavaScript 从 1 开始迭代?

[英]How I can start Iteration from 1 Using Object.entries() method JavaScript?

const game = {
  team1: 'Bayern Munich',
  team2: 'Borrussia Dortmund',
  players: [
    [
      'Neuer',
      'Pavard',
      'Martinez',
      'Alaba',
      'Davies',
      'Kimmich',
      'Goretzka',
      'Coman',
      'Muller',
      'Gnarby',
      'Lewandowski',
    ],
    [
      'Burki',
      'Schulz',
      'Hummels',
      'Akanji',
      'Hakimi',
      'Weigl',
      'Witsel',
      'Hazard',
      'Brandt',
      'Sancho',
      'Gotze',
    ],
  ],
  score: '4:0',
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'],
  date: 'Nov 9th, 2037',
  odds: {
    team1: 1.33,
    x: 3.25,
    team2: 6.5,
  },
};

1) Loop over the game.scored array and print each player name to the console,along with the goal number start from 1 like (Goal 1: etc) 1)遍历 game.scored 数组并将每个玩家姓名打印到控制台,以及从 1 开始的目标编号(如目标 1:等)

This is my first solution and the result is perfect:这是我的第一个解决方案,结果很完美:

for (const [i, v] of game.scored.entries()) 
{
  console.log(`Goal: ${i + 1} ${v}`);
}

Output: Output:

Goal: 1 Lewandowski

Goal: 2 Gnarby

Goal: 3 Lewandowski

Goal: 4 Hummels

Picture Result:图片结果:在此处输入图像描述

The issue is when I tried from different method using Object.entries() the itration don't start from 1问题是当我使用 Object.entries() 从不同的方法尝试时,迭代不是从 1 开始

Soultion 2灵魂 2

for (const [i, v] of Object.entries(game.scored)) 
{
  console.log(`Goal: ${i+1} ${v}`);
}

Output: Output:

Goal: 01 Lewandowski

Goal: 11 Gnarby

Goal: 21 Lewandowski

Goal: 31 Hummels

Picture Result:图片结果:在此处输入图像描述

The first approach takes the array and its entries ( Array#entries ), whereas the second one takes the object and the entries from the object ( Object.entries ).第一种方法采用数组及其条目( Array#entries ),而第二种方法采用 object 和 object 中的条目( Object.entries )。

Objects has only strings or symbols as keys.对象只有字符串或符号作为键。 To take a number as value you need to convert it to a number.要将数字作为值,您需要将其转换为数字。 The shortest way is to take an unary plus + .最短的方法是采用一元加号+

 const game = { team1: 'Bayern Munich', team2: 'Borrussia Dortmund', players: [['Neuer', 'Pavard', 'Martinez', 'Alaba', 'Davies', 'Kimmich', 'Goretzka', 'Coman', 'Muller', 'Gnarby', 'Lewandowski'], ['Burki', 'Schulz', 'Hummels', 'Akanji', 'Hakimi', 'Weigl', 'Witsel', 'Hazard', 'Brandt', 'Sancho', 'Gotze']], score: '4:0', scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'], date: 'Nov 9th, 2037', odds: { team1: 1.33, x: 3.25, team2: 6.5 } }; for (const [i, v] of Object.entries(game.scored)) { console.log(`Goal: ${+i + 1} ${v}`); }

game.scored.entries() returns an iterator object in which the indexes are returned as numbers. game.scored.entries()返回一个迭代器 object,其中索引以数字形式返回。 So adding 1 to the value of i performs the addition and gives you the expected result.因此,将i的值加 1 会执行加法并给出预期的结果。

Object.entries(game.scored) returns the key as a string. Object.entries(game.scored)key作为字符串返回。 So when you do i + 1 in the second code example, instead of doing an addition, your code performs concatenation.因此,当您在第二个代码示例中执行i + 1时,您的代码不会执行加法,而是执行连接。 Since i is a string, 1 will be converted to string and then concatenated with the value of i .由于i是一个字符串,因此 1 将被转换为字符串,然后与i的值连接。

To get the desired result, convert i to a number before adding 1 to it.要获得所需的结果,请将i转换为数字,然后再将其加 1。

There are multiple ways to convert a string to a number.有多种方法可以将字符串转换为数字。 You can use one of the following ways:您可以使用以下方法之一:

  • Using + unary operator使用+一元运算符

    for (const [i, v] of Object.entries(game.scored)) { console.log(`Goal: ${(+i)+1} ${v}`); }
  • Using parseInt() function使用parseInt() function

     for (const [i, v] of Object.entries(game.scored)) { console.log(`Goal: ${parseInt(i) + 1} ${v}`); }
  • Using Number() constructor使用Number() constructor

     for (const [i, v] of Object.entries(game.scored)) { console.log(`Goal: ${Number(i) + 1} ${v}`); }

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

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