Just came to add a cleaner system by default:
const describePerson = (name, ...traits) => { traits = Object.assign(['x', 'y'], traits); return 'Hi, ${name}, you are ${traits.join(', ')}'; } describePerson('z');
This works because arrays are objects whose indices are keys, and Object.assign replaces the keys of the first object present in the second with the values ββof the second.
If the second one does not have index 1, then it will not be overwritten, but if it has index 0, the index of the first array 0 will be overwritten by the second, which is the behavior that you expect in default.
Note that array expansion is not the same as object expansion, so [....['x', 'y'],...traits] will not overwrite indexes
source share