Parse and modify an array in Javascript
Thought these two operation can be done together for clean writing you can do the following.
map
to add the difference between the two property in a new property called d
using Math.abs
to keep the difference positive.
let teams = [{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}];
teams.map((t) => t['d'] = Math.abs(t.f - t.a))
console.log(teams);
To reduce to the team that has the lowest difference you can use the below code with reduce
let teams = [{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}];
teams.map((t) => t['d'] = Math.abs(t.f - t.a))
// now finding the team with the lowsest
let result = teams.reduce((teamA,teamB) => teamA.d > teamB.d? teamB: teamA );
console.log(result);
Both using loop (e.g. for...of ) or array methods (e.g. map for the part 1 and filter for part 2) are valid approach.
I personally prefer loop because it is easier to read and debug and you can combine both steps into one loop. something like:
// use the first element as starter then update in the loop
let leastDiff = Math.abs(myArray[0].f - myArray[0].a);
let bestTeam = myArray[0].team;
for (let item of myArray) {
item.diff = Math.abs(item.f - item.a);
if (item.diff < leastDiff) {
leastDiff = item.diff;
bestTeam = item.team;
}
}
console.log(`Team with least difference is ${bestTeam} with ${leastDiff} goal difference`);
A combination of map and reduce will do:
const teams= [{"team":"Arsenal","f":"79","a":"36"}, {"team":"Liverpool","f":"67","a":"30"}, {"team":"Manchester_U","f":"87","a":"45"}, {"team":"Newcastle","f":"74","a":"52"}, {"team":"Leeds","f":"53","a":"37"}, {"team":"Chelsea","f":"66","a":"38"}, {"team":"West_Ham","f":"48","a":"57"}, {"team":"Aston_Villa","f":"46","a":"47"}, {"team":"Tottenham","f":"49","a":"53"}, {"team":"Blackburn","f":"55","a":"51"}, {"team":"Southampton","f":"46","a":"54"}, {"team":"Middlesbrough","f":"35","a":"47"}, {"team":"Fulham","f":"36","a":"44"}, {"team":"Charlton","f":"38","a":"49"}, {"team":"Everton","f":"45","a":"57"}, {"team":"Bolton","f":"44","a":"62"}, {"team":"Sunderland","f":"29","a":"51"}, {"team":"Ipswich","f":"41","a":"64"}, {"team":"Derby","f":"33","a":"63"}, {"team":"Leicester","f":"30","a":"64"}]
let diff=Infinity;
console.log(
teams.map((t)=>(t.d=t.a-t.f, t))
.reduce((a,c)=>Math.abs(c.d)<=diff?(diff=Math.abs(c.d), c.team): a,'')
);