Best way Merge two string arrays based on a condition

Viewed 47

Hello I want to merge two string arrays.

Array 1:
0: "Cloud Infrastructure"
1: "Cloud Infrastructure"
2: "IaaS"
3: "IaaS"
4: "Infrastructure"
5: "Infrastructure"
6: "Compute"
7: "Compute"
8: "Compute"
9: "Compute"

Array 2:
0: "hideValue123"
1: "hideValue123"
2: "hideValue123"
3: "hideValue123"
4: "hideValue123"
5: "hideValue123"
6: "Networking"
7: "Networking"
8: "Networking"
9: "Networking"

What I want is in the final array. Array three to have all elements of array two that are different from hideValue123. And for those elements that is hideValue123, I want to get them from array 1. I want the order/index to be the same:

Array 3 :
0: "Cloud Infrastructure"
1: "Cloud Infrastructure"
2: "IaaS"
3: "IaaS"
4: "Infrastructure"
5: "Infrastructure"
6: "Networking"
7: "Networking"
8: "Networking"
9: "Networking"
1 Answers

const array1 = ["Cloud Infrastructure",
    "Cloud Infrastructure",
    "IaaS",
    "IaaS",
    "Infrastructure",
    "Infrastructure",
    "Compute",
    "Compute",
    "Compute",
    "Compute"]
    
const array2 = [
    "hideValue123",
    "hideValue123",
    "hideValue123",
    "hideValue123",
    "hideValue123",
    "hideValue123",
    "Networking",
    "Networking",
    "Networking",
    "Networking",
]
const array3 = array2.map((item, index) => item === "hideValue123" ? array1[index] : item);
console.log(array3);

Related