I have a scenario where I need to update an array with values from another dynamic array. The key point to consider here is that the order or indices of the elements are not important and may not be sequential.
arrayA = [
{name: 'John Doe', sport: 'soccer', point: 20},
{name: 'Jack Kim', sport: 'tennis', point: 10},
{name: 'Tiger Wood', sport: 'golf', point: 22},
{name: 'Lewis Hamilton', sport: 'F1', point: 30},
]
The arrayB contains different point values and can vary in size.
arrayB = [
{name: 'John Doe', sport: 'soccer', point: 50},
{name: 'Jack Kim', sport: 'tennis', point: 100},
]
The resulting newArray will combine both arrays, updating values from arrayB if there's a match.
newArray = [
{name: 'John Doe', sport: 'soccer', point: 50},
{name: 'Jack Kim', sport: 'tennis', point: 100},
{name: 'Tiger Wood', sport: 'golf', point: 22},
{name: 'Lewis Hamilton', sport: 'F1', point: 30}]
I am seeking an optimal way to achieve this rather than my initial idea of using slice and push methods which might not be efficient.
NOTE: This is more of an algorithmic explanation and I'm open to suggestions on improving the approach.
let newArray = [];
arrayA.map(v => {
if(v.sport === arrayB.sport) {
index = arrayA.findIndex(v.sport)
arrayA.slice(index);
newArray = arrayA;
}
});
newArray.push(arrayB)