0

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"]
3
  • 5
    huh???????????? Commented Sep 12, 2019 at 15:37
  • 1
    Can you clarify your question? As it is, it is hard to understand what you are asking Commented Sep 12, 2019 at 15:38
  • If we compare this two array , index 0 and 8 of array1 is equal to array2, then change the value of that indexes with new values in array1. my new array1 = [0, "Feb", "March", ..., 0, "Oct",... , "Dec"] Commented Sep 12, 2019 at 15:41

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have an another problems, array1, array2 are same and there is an array3 is same length as array2, now how to set array3 value to array1 instead of 0 .
@VinitBhavsar It would be similar to this answer, but instead of setting the array item to 0, you would set it like this: array1[i] = array3[i];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.