In my react app, I currently have two arrays with objects that I would like to combine.
const cars = [
{'car': 'porsche', 'description': 'luxury', 'driverId': 4},
{'car': 'ferrari', 'description': 'beatiful', 'driverId': 2},
{'car': 'bmw', 'description': 'slow', 'driverId': 5}
]
const drivers = [
{'driver':{'car': 'kia', 'name':'John' , 'description': 'Overweight'}},
{'driver':{'car': 'toyota', 'name':'Koko' , 'description': 'Ugly looking'}},
{'driver':{'car': 'nissan', 'name':'Goku' , 'description': 'Tall'}}
]
After combining two objects, I would like end result to be like this.
const carInfo = [
{'car': 'porsche', 'description': 'luxury', 'driverId': 4, 'carName':'kia', 'name':'John' , 'descriptionDriver': 'Overweight'},
{'car': 'ferrari', 'description': 'beatiful', 'driverId': 2, 'carName': 'toyota', 'name':'Koko' , 'descriptionDriver': 'Ugly looking'},
{'car': 'bmw', 'description': 'slow', 'driverId': 5, 'carName': 'nissan', 'name':'Goku' , 'descriptionDriver': 'Tall'}
]
Basically, I would like to change the key name in drivers object then combine them. Because spread operator wouldn't work when there are same key name between objects. How can I get this result?