how to replace list of elements in an array with an index value, if the element has a specific prefix of Xyz in javascript. I need to find the items in an array that has john as a prefix and replace them with the index values. note: only string with a prefix of xyz is valid for example input: arrNames[john, jack, johnsmith, funjack, john]. output: [john,jack,2,funjack,john]
I tried with the indexOf method but didn't get the expected result.
Hope this is your goal
const arrNames=['john', 'jack', 'johnsmith', 'funjack', 'john'];
arrNames.map(x => x.startsWith('john') && x !== 'john' ? arrNames.findIndex(f => f===x):x)