I have two arrays, the first contains two numbers and the second 12 values.
firstArray = [0, 6];
secondArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
I want to take the numbers of the first array and and check that index of the second array matches and then change the value of the matching ones.
In this case the output would be
secondArray = ["blank", "February", "March", "April", "May", "June", "blank", "August", "September", "October", "November", "December"];
I was trying to for-loop for getting the values in firstArray but I'm gettin the indexes and not the value
for (let i = 0; i < this.state.lastIndex.length; i++) {
}
hay How can I do it? Is there any way to make it more direct than with a for-loop?
thanks for your help
For a oneliner, you could use map from Array.prototype
:
secondArray = secondArray.map((element, index) => firstArray.includes(index) ? "blank" : element )
Please note the map function returns a new array, instead of modifying the original one.
Edit: removed redundant return
and curly-braces around the arrow function body
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments