-3

I've already looked at How do I remove a particular element from an array in JavaScript? but this doesn't quite answer my question as I can't get it to work.

I have an array

var cardDeck = [{
    "card": "Ace",
    "suit": "Diamonds",
    "color": "Red",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Spades",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Clubs",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Hearts",
    "color":"Red",
    "eval": 1,
    "altEval":11
  }
    ]
;

console.log(cardDeck);

and I want to remove create a new array with one of the objects.

In plain language, this is a card deck with 4 aces, I want to take out one of the aces and put it in a new array (playerhand)

///
var cardDeck = [{
    "card": "Ace",
    "suit": "Diamonds",
    "color": "Red",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Spades",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Clubs",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Hearts",
    "color":"Red",
    "eval": 1,
    "altEval":11
  }
    ]
;

//console.log(cardDeck);

var playerhand = cardDeck.splice[0];

console.log(playerhand)

The above code logs undefined.

var array = [2, 5, 9];
console.log(array)

array.splice(array, [1]);

//array = [2, 9]
console.log(array);

The above works but the following does not:

var array = [{
    "card": "Ace",
    "suit": "Diamonds",
    "color": "Red",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Spades",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Clubs",
    "color":"Black",
    "eval": 1,
    "altEval":11
  },
     {
    "card": "Ace",
    "suit": "Hearts",
    "color":"Red",
    "eval": 1,
    "altEval":11
  }
    ];
console.log(array);

array.splice(array, [0]);


console.log(array);
Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
aaron lilly
  • 274
  • 1
  • 13
  • 2
    `cardDeck.splice[0]`, `splice(array, [0])`, ..., these all don't make much semantical sense in almost all scenarios. Please read another JS tutorial, i think it would be the most beneficial. – ASDFGerte Oct 17 '19 at 16:47
  • `.splice` is a *method*, you call it the same way you call functions and other methods with `()` at the end. `cardDeck.splice[0];` doesn't work that way. And `array.splice(array, [0]);` doesn't pass the correct arguments to the method. – VLAZ Oct 17 '19 at 16:48
  • Please go through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – Ayush Gupta Oct 17 '19 at 16:48
  • that helps with splice, guess i need to find the correct method. – aaron lilly Oct 17 '19 at 16:53
  • `splice` is the correct method to remove an item from an array at a specific index and return it in a new array. – Heretic Monkey Oct 17 '19 at 17:23
  • The linked question's accepted answer formulates the `splice` method correctly, and explains what the parameters do and what is returned, in addition to linking to the same resource as @AyushGupta. – Heretic Monkey Oct 17 '19 at 17:29

1 Answers1

2

array.splice(0,1,"") this will work fine.

splice method takes as array.splice(index, howmany, item1, ....., itemX) index to delete, how many from the given index to delete and the 3 parameter is what to add in place of that(which you can skip)

Ayushi Keshri
  • 719
  • 5
  • 17