一、從JavaScript數組中刪除另一個數組的元素
如果我們想要從一個JavaScript數組中刪除另一個數組的元素,我們可以使用Array.filter()方法。這個方法允許我們將需要刪除的元素過濾出來,然後將它們從原始數組中移除。
const originalArray = [1, 2, 3, 4, 5]; const removeArray = [2, 5]; const resultArray = originalArray.filter(item => !removeArray.includes(item)); console.log(resultArray); // [1, 3, 4]
在上面的例子中,我們聲明了一個原始數組和一個需要從中刪除的數組。我們使用Array.filter()方法和Array.includes()方法,將需要刪除的元素過濾出來並從原始數組中移除。最後,我們將過濾後的數組賦給resultArray。
二、在JavaScript數組中添加另一個數組
如果我們想要將一個JavaScript數組添加到另一個數組中,我們可以使用Array.concat()方法。
const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const resultArray = array1.concat(array2); console.log(resultArray); // [1, 2, 3, 4, 5, 6]
在上面的例子中,我們聲明了兩個數組,並使用Array.concat()方法將它們合併成一個新的數組。最後,我們將合併後的數組賦給resultArray。
三、從JavaScript數組中刪除最後一個元素
如果我們想要從一個JavaScript數組中刪除最後一個元素,我們可以使用Array.pop()方法。
const originalArray = [1, 2, 3, 4, 5]; const lastElement = originalArray.pop(); console.log(lastElement); // 5 console.log(originalArray); // [1, 2, 3, 4]
在上面的例子中,我們聲明了一個原始數組,並使用Array.pop()方法將最後一個元素從數組中刪除。最後,我們將刪除後的數組賦給originalArray,並將刪除的元素賦給lastElement。
四、將JavaScript數組賦值給另一個數組
如果我們想將一個JavaScript數組賦值給另一個數組,我們可以使用JavaScript Spread操作符。
const originalArray = [1, 2, 3, 4, 5]; const newArray = [...originalArray]; console.log(newArray); // [1, 2, 3, 4, 5]
在上面的例子中,我們聲明了一個原始數組,並使用Spread操作符將它賦給新數組newArray。
五、將JavaScript數組拼接到另一個數組
如果我們想要將一個JavaScript數組拼接到另一個數組的末尾,我們可以使用Array.push()方法或Array.concat()方法。
const originalArray = [1, 2, 3]; const newArray = [4, 5, 6]; originalArray.push(...newArray); console.log(originalArray); // [1, 2, 3, 4, 5, 6] const anotherArray = [7, 8, 9]; const resultArray = originalArray.concat(anotherArray); console.log(resultArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
在上面的例子中,我們聲明了一個原始數組和一個要拼接的數組。我們使用Array.push()方法將要拼接的數組添加到原始數組的末尾,或者使用Array.concat()方法將兩個數組拼接到一起。
六、從JavaScript數組中過濾另一個數組的元素
如果我們想要過濾出一個JavaScript數組中另一個數組的元素,我們可以使用Array.filter()方法和Array.includes()方法。
const originalArray = [1, 2, 3, 4, 5]; const filterArray = [2, 4]; const resultArray = originalArray.filter(item => filterArray.includes(item)); console.log(resultArray); // [2, 4]
在上面的例子中,我們聲明了一個原始數組和一個要過濾的數組。我們使用Array.filter()方法和Array.includes()方法,將要過濾的元素過濾出來,最後將過濾出的元素賦給resultArray。
七、將JavaScript數組複製到另一個數組
如果我們想要將一個JavaScript數組複製到另一個數組中,我們可以使用Array.slice()方法。
const originalArray = [1, 2, 3, 4, 5]; const copiedArray = originalArray.slice(); console.log(copiedArray); // [1, 2, 3, 4, 5]
在上面的例子中,我們聲明了一個原始數組,並使用Array.slice()方法將它複製給另一個數組copiedArray。
八、將一個JavaScript數組放入另一個數組中
如果我們想要將一個JavaScript數組放入另一個數組中,我們可以使用Array.push()方法或Array.concat()方法。
const originalArray = [1, 2, 3]; const newArray = [4, 5, 6]; originalArray.push(newArray); console.log(originalArray); // [1, 2, 3, [4, 5, 6]] const anotherArray = [7, 8, 9]; const resultArray = originalArray.concat(anotherArray); console.log(resultArray); // [1, 2, 3, [4, 5, 6], 7, 8, 9]
在上面的例子中,我們聲明了一個原始數組和一個要放入的數組。我們使用Array.push()方法將要放入的數組添加到原始數組的末尾,或者使用Array.concat()方法將兩個數組拼接到一起。
九、從JavaScript數組中刪除某個元素
如果我們想要從一個JavaScript數組中刪除某個元素,我們可以使用Array.splice()方法。
const originalArray = [1, 2, 3, 4, 5]; originalArray.splice(2, 1); console.log(originalArray); // [1, 2, 4, 5]
在上面的例子中,我們聲明了一個原始數組,並使用Array.splice()方法將第三個元素(索引為2)刪除。最後,我們將刪除後的數組賦給originalArray。
十、JavaScript數組刪除另一個數組
如果我們想要從一個JavaScript數組中刪除另一個數組,我們可以使用前面介紹的Array.filter()方法。方式是在callback函數中使用Array.includes()方法判斷元素是否需要刪除。
const originalArray = [1, 2, 3, 4, 5]; const removeArray = [2, 5]; const resultArray = originalArray.filter(item => !removeArray.includes(item)); console.log(resultArray); // [1, 3, 4]
在上面的例子中,我們聲明了一個原始數組和一個需要從中刪除的數組。我們使用Array.filter()方法和Array.includes()方法,將需要刪除的元素過濾出來並從原始數組中移除。最後,我們將過濾後的數組賦給resultArray。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/249289.html