Is there a way to update a property in object array based on the number of times some other property is present as element in some other array
I have 2 arrays, array1 and array2:
var array1 = ["JOHN", "JACK", "JACK"]; var array2 = [ {count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"} ]; Expected output :
[ {count: 7, value: "JACK"}, // count = 9 - 2 {count: 8, value: "JOHN"}, // count = 9 - 1 {count: 2, value: "TEST"} ] In array1, "JACK" is present twice, so I need to reduce count by 2, similarly "JOHN" is present once and hence its reduced by 1, "TEST" is not present so unchanged.
I tried the following
array1.map(item => { return array2.find( p => p["value"] === item); }); With this, I am getting the below output,
[ {count: 9, value: "JOHN"}, {count: 9, value: "JACK"}, {count: 9, value: "JACK"} ] I am not sure whether it can be achieved using single lambda expression.
Thanks in advance!