简体   繁体   English

在对象数组上拼接功能

[英]Splice function on an array of objects

I'm doing a react/javascript exercice and i'm having some trouble understanding the use of splice() in it. 我正在做一个react / javascript练习,并且在理解splice()的使用时遇到了一些麻烦。 I have 8 cards, and i need to randomly assign 4 cards to 2 players. 我有8张牌,我需要向4位玩家随机分配4张牌。 Now everything works fine, but i don't understand the [0] at the end of the let randPokemon = hand2.splice(randIndex, 1)[0]; 现在一切正常,但我不明白的[0]在年底let randPokemon = hand2.splice(randIndex, 1)[0]; line. 线。

Here is the full code : 这是完整的代码:

import React, { Component } from "react";
import Pokedex from "./Pokedex";

class Pokegame extends Component {
  static defaultProps = {
    pokemon: [
      { id: 4, name: "Charmander", type: "fire", experience: 62 },
      { id: 7, name: "Squirtle", type: "water", experience: 63 },
      { id: 11, name: "Metapod", type: "bug", experience: 72 },
      { id: 12, name: "Butterfree", type: "flying", experience: 178 },
      { id: 25, name: "Pikachu", type: "electric", experience: 112 },
      { id: 39, name: "Jigglypuff", type: "normal", experience: 95 },
      { id: 94, name: "Gengar", type: "poison", experience: 225 },
      { id: 133, name: "Eevee", type: "normal", experience: 65 }
    ]
  };

  render() {
    let hand1 = [];
    let hand2 = [...this.props.pokemon];

    while (hand1.length < hand2.length) {
      let randIndex = Math.floor(Math.random() * hand2.length);
      let randPokemon = hand2.splice(randIndex, 1)[0];
      hand1.push(randPokemon);
    }

    console.log(hand1);
    console.log(hand2);

    return (
      <div className="Pokegame">
        <h1>Pokegame!</h1>
      </div>
    );
  }
}

export default Pokegame;

I understand (correct me if i'm wrong) that the splice() function can take 2 or more arguments : the first one is the index telling what position to add/remove items, the second one is the number of items to add/remove, and the next arguments being the items we want to add (but i don't use it here since i only want to remove the selected item and add it to the first hand). 我了解(如果我错了,请纠正我) splice()函数可以接受2个或更多参数:第一个是告诉添加/删除项目的位置的索引,第二个是添加/删除项目的数量。删除,下一个参数是我们要添加的项目(但我在这里不使用它,因为我只想删除所选项目并将其添加到第一手)。

Now in this case, i'm having trouble to understand how that [0] works or why it's here... 现在在这种情况下,我很难理解[0]工作原理或为什么在这里...

The splice -method adds or removes items from an array. splice方法可以从数组中添加或删除项目。 It also returns always an array-object, so the [0] is to access the first element in that array created/modified by splice. 它还总是返回一个数组对象,因此[0]将访问由splice创建/修改的数组中的第一个元素。

If you take a look at the splice reference you'll see that it actually returns an array of items that were removed by the operation. 如果看一下拼接引用,您会发现它实际上返回了该操作删除的项目数组。

So in this case it would remove one pokemon from the hand2 array at the random index and return an array with one item. 因此,在这种情况下,它将从hand2数组中的随机索引处删除一个口袋妖怪,并返回一个包含一项的数组。

[0] takes that item out of the array ad places it in the randPokemon variable. [0]将该项目从数组广告中取出,将其放置在randPokemon变量中。

Because splice always returns an array - the [0] extracts the first (and only) element. 因为splice总是返回一个数组- [0]提取第一个(也是唯一的)元素。 You could use destructuring to achieve the same purpose: 您可以使用解构来实现相同的目的:

let [randPokemon] = hand2.splice(randIndex, 1);

According to MDN the return value of the splice() is array consisting of removed elements from array. 根据MDNsplice()的返回值是一个数组,其中包含从数组中删除的元素。

Return Value 返回值

An array containing the deleted elements. 包含已删除元素的数组。 If only one element is removed, an array of one element is returned. 如果仅删除一个元素,则返回一个元素的数组。 If no elements are removed, an empty array is returned 如果没有删除任何元素,则返回一个空数组

Its just a way to get the removed element of the array. 这只是获取数组中已删除元素的一种方法。 hand2.splice(randIndex, 1) will return the removed elements from array. hand2.splice(randIndex, 1)将返回从数组中删除的元素。 So in this case there is only 1 element so [0] will get that first element. 因此,在这种情况下,只有1个元素,因此[0]将获得第一个元素。

Array.splice() gives you the new spliced array. Array.splice()为您提供了新的拼接数组。 Even though there is only one value in the array, it's still an array, so to access the variable, you would use [0]. 即使数组中只有一个值,它仍然是一个数组,因此要访问变量,您可以使用[0]。

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

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