Vinit Bhavsar
Vinit Bhavsar

Reputation: 254

Compare two arrays and at same value index change data

I've an array like this:

    array1 = ["Jan","Feb",....,"Dec"];
    array2 = ["Jan","Sep"];

Now compare both and at array1[0] and array1[8] values changed to 0.

I want output:

    array1 = [0, "Feb", "March", ..., 0, "Oct", ..., "Dec"]

Upvotes: 0

Views: 871

Answers (1)

Jose Nuno
Jose Nuno

Reputation: 608

Not sure if I understood correctly, but assuming you want to change the array1 values to 0, if they are contained in array2, you could do this:

array1 = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
array2 = ["Jan","Sep"];

for (let i = 0; i < array1.length; i++) {
    if (array2.includes(array1[i])) {
        array1[i] = 0;
    }
}

console.log(array1);

Upvotes: 4

Related Questions