簡體   English   中英

如何有條件地將只有數字的項目從一個數組移動到另一個數組

[英]How to conditionally move only items with numbers from one array to another array jquery

我正在尋找幫助,弄清楚如何檢查數組中是否有數字的元素,然后將這些項目移動到另一個數組中。

    var array = ["January 2018", "Item One", "Item Two", "December 2017"],
    array2 = [];
    if(
      //array item contains a number 
    ){
    //array item with number move to array2
    }

array未正確硬編碼。 在此先感謝您的幫助。

 var array = ["January 2018", "Item One", "Item Two", "December 2017"]; // Make new array containing only the elements that have digits var array2 = array.filter( x => /\\d/.test(x) ); console.log( array2 ); 

這將創建一個新數組,其中包含帶有數字的元素,並將其從初始數組中刪除。

  var array = ["January 2018", "Item One", "Item Two", "December 2017"]; var numbersArray = array.filter(function(item, idx){ var regx = /\\d/g; regx.test(item) && array.splice(idx, 1); return regx.test(item); }); console.log(array); console.log(numbersArray); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM