I have an array of objects of people, which contains:
- id (of the person)
- name (of the person)
- likes (id of the person they like)
.
array = [{ id:1, name:"Adam", likes:2 },{ id:2, name:"Ben", likes:3 },{ id:3, name:"Cindy", likes:1 }];
The final array should have the id of the person they like replaced with their name
array = [{ id:1, name:"Adam", likes:"Ben" },{ id:2, name:"Ben", likes:"Cindy" },{ id:3, name:"Cindy", likes:"Adam" }];
Current Solution:
for(let i=0;i<array.length;i++){
for(let j=0;array.length;j++){
if(array[i].likes == array[j].id){
array[i].likes = array[j].name;
};
};
};
Is there a more efficient way of doing this other than using 2 loops?