我已经看过How do I remove a particular element from an array in JavaScript?,但这不能完全回答我的问题,因为我无法使用它。
我有一个数组
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);
我想删除其中一个对象创建新数组。
用简单的语言来说,这是一个有4个A的纸牌,我想取出一个A并放入一个新的数组(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)
以上代码记录了undefined
。
var array = [2, 5, 9];
console.log(array)
array.splice(array, [1]);
//array = [2, 9]
console.log(array);
以上方法有效,但以下方法无效:
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);
答案 0 :(得分:1)
array.splice(0,1,“”)可以正常工作。
splice方法采用array.splice(index,howmany,item1,.....,itemX) 要删除的索引,要删除的给定索引中的索引数以及要替换的3参数(可以跳过)