Yerlan Yeszhanov
Yerlan Yeszhanov

Reputation: 2439

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"]

Upvotes: 0

Views: 26

Answers (2)

The Reason
The Reason

Reputation: 7973

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)

Upvotes: 1

Polynomial Proton
Polynomial Proton

Reputation: 5135

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);

Upvotes: 1

Related Questions