简体   繁体   中英

How to find element in array which contains date?

I have an array , each element contains date , cost, currency and name info:

a = [" 2017-04-25 2 USD Jogurt", " 2017-04-28 2 USD Milk", " 2019-04-25 2 USD 
Apple"]  

I'm trying loop through an array find and delete element with the date of 2019-04-25 , but I can't find it

var indexes = [];
var index;

for (index = 0; index < a.length; ++index) {
    if (a[index] === "2019-04-25") {
        rea.splice (index, 1);
    }
};
console.log(a);

>> (3) [" 2017-04-25 2 USD Jogurt", " 2017-04-28 2 USD Milk", " 2019-04-25 2 USD Apple"]

You can use includes to check if string exists. Check below snippet.

 var a = [" 2017-04-25 2 USD Jogurt", " 2017-04-28 2 USD Milk", " 2019-04-25 2 USD Apple"] ; for (var index = 0; index < a.length; ++index) { if (a[index].includes("2019-04-25")) { a.splice (index, 1); } }; console.log(a); 

Simply using Array.prototype.filter & String.prototype.includes methods like so:

 const array = [" 2017-04-25 2 USD Jogurt", " 2017-04-28 2 USD Milk", " 2019-04-25 2 USD Apple"]; const result = array.filter(d => !d.includes('2019-04-25')) // your date console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM