I'm new to javascript and trying to learn some basic with toy examples.
Say I have an array containing data on six people.
const myArray = [
{"id": 1, "value": 75, "friends": 3},
{"id": 2, "value": 40, "friends": 4},
{"id": 3, "value": 60, "friends": 5},
{"id": 4, "value": 62, "friends": 6},
{"id": 5, "value": 55, "friends": 1},
{"id": 6, "value": 33, "friends": 2}
];
The array lists each person's id, value, and who they're friends with. E.g., Person 1 is friends with person 3, and person 3 is friends with person 5, and so on.
Now I want to manipulate who is friends with who based on each person's value. This is the logic I'd like to implement (probably in a for loop):
IF person's value is the lowest or the second lowest value in the array, THEN add the id of the person with the highest value in the array to their friends.
So my desired output in this case is:
const myArray = [
{"id": 1, "value": 75, "friends": 3},
{"id": 2, "value": 40, "friends": [4, 1]},
{"id": 3, "value": 60, "friends": 5},
{"id": 4, "value": 62, "friends": 6},
{"id": 5, "value": 55, "friends": 1},
{"id": 6, "value": 33, "friends": [2, 1]}
];
How can I do this?
I've done a very basic manipulation of the array below, where I take away the friend of the person with the highest value in the array. But I've gotten confused as I've moved onto this more complicated task.
const myArray = [
{"id": 1, "value": 75, "friends": 3},
{"id": 2, "value": 40, "friends": 4},
{"id": 3, "value": 60, "friends": 5},
{"id": 4, "value": 62, "friends": 6},
{"id": 5, "value": 55, "friends": 1},
{"id": 6, "value": 33, "friends": 2}
];
// Finds max and min values in array
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
tmp = myArray[i].value;
if (tmp > highest) highest = tmp;
};
for(i = 0; i < myArray.length; i++){
// If person has the highest value in the array
if(myArray[i].value == highest){
// Then take away their friend
myArray[i].friends = NaN
} else {
myArray[i].friends = myArray[i].friends
}
};
console.log(myArray);