Вы можете l oop через массив и выполнить логи c, чтобы добавить и удалить нужные элементы. Я добавил встроенные комментарии, пожалуйста, проверьте и дайте мне знать:
let positions = [{id: "pos1", name:"pos1"}, {id: "pos2", name:"pos2"}, {id: "pos4", name:"pos4"}]
let paritcipants = {pos1:[{salary:1000}],pos3:[{salary:1500}]};
debugger;
// Retrieve the keys of paritcipants ['pos1', 'pos3']
const paritcipantsKeys = Object.keys(paritcipants);
/* Iterate over positions and check if they exist in paritcipants
if they are not in it then push it.
else remove it from keys array so that you can know which are extra keys in paritcipants
*/
for (const position of positions) {
if( paritcipantsKeys.indexOf(position.id) === -1) {
paritcipants[position.id] = [{salary: 1000}];
} else {
paritcipantsKeys.splice(paritcipantsKeys.indexOf(position.id), 1)
}
}
// Remove extra keys from paritcipants
for(const paritcipantsKey of paritcipantsKeys) {
delete paritcipants[paritcipantsKey];
}
console.log(paritcipants);