試圖從陣列中洗掉特定值。但無法洗掉,因為陣列是這樣的
var cardValue = {
options: ['Bus', 'Car', 'Motor'],
};
那么,如何洗掉該陣列的值?
const appDiv = document.getElementById('app');
appDiv.innerHTML = `<h1>JS Starter</h1>`;
var cardValue = {
options: ['Bus', 'Car', 'Motor'],
};
const index = this.cardValue.options.indexOf('Bus');
if (index > -1) {
this.cardValue.options.splice(index, 1);
}
console.log(cardValue); // Car, Motor
<div id="app"></div>
演示:https ://stackblitz.com/edit/js-a1jxhx?file=index.js
uj5u.com熱心網友回復:
this在訪問cardValue物件之前不應該使用
var cardValue = {
options: ['Bus', 'Car', 'Motor'],
};
const index = cardValue.options.indexOf('Bus');
if (index > -1) {
cardValue.options.splice(index, 1);
}
console.log(cardValue); // Car, Motor
uj5u.com熱心網友回復:
const appDiv = document.getElementById('app');
appDiv.innerHTML = `<h1>JS Starter</h1>`;
var cardValue = {
options: ['Bus', 'Car', 'Motor'],
};
const index = this.cardValue.options[0];
console.log(cardValue); // Car, Motor
<div id="app"></div>
uj5u.com熱心網友回復:
var list = ["bar", "baz", "foo", "qux"];
list.splice(0, 2);
// 從索引位置 0 開始,洗掉兩個元素 ["bar", "baz"] 并保留 ["foo", "qux"]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492940.html
標籤:javascript
下一篇:JS使用物件動態鍵名
