[英]How to add an object to an array based on its values?
I have an array of objects, it looks like this:我有一个对象数组,它看起来像这样:
[{
Rank: 1,
Speed1: 91,
Speed2: 457,
Username: 'monizotic',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}, {
Rank: 2,
Speed1: 91,
Speed2: 457,
Username: 'Thachtawan',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}, {
Rank: 3,
Speed1: 91,
Speed2: 456,
Username: 'PassornSibpang',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}, {
Rank: 4,
Speed1: 91,
Speed2: 456,
Username: 'WasinSoikeeree',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}, {
Rank: 5,
Speed1: 91,
Speed2: 454,
Username: 'user1055644',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}]
Each object is "1 user".每个 object 是“1 个用户”。 Rank is the user's place in the leaderboard.
排名是用户在排行榜中的位置。 Each user also has Speed1 and Speed2 .
每个用户也有Speed1和Speed2 。 Whoever has the highest Speed1 and Speed2 is in the first place.
Speed1和Speed2最高的人排在第一位。 Those who have the lowest ones are on the last place.
那些最低的人排在最后。 I hope you understand the meaning.
我希望你明白其中的意思。 I need to somehow implement a function that would be able to insert a "new user" into this array with objects.
我需要以某种方式实现一个 function ,它能够将一个“新用户”插入到这个带有对象的数组中。 An example of such a user:
此类用户的示例:
{
Rank: null,
Speed1: 91,
Speed2: 456,
Username: 'thaniman',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}
In the object above Rank is null, since it is not yet known.在 object 上面的 Rank 是 null,因为它还不知道。 It should change when this user object is in the right place in the array
当此用户 object 在数组中的正确位置时,它应该会改变
How can I do that?我怎样才能做到这一点?
Please see the below solution with an O(N)
time complexity.请参阅以下具有
O(N)
时间复杂度的解决方案。
It was made with an assumption that the desired insertion index logic is (user.Speed1 * user.Speed2) < (newUser.Speed1 * newUser.Speed2)
.假设所需的插入索引逻辑是
(user.Speed1 * user.Speed2) < (newUser.Speed1 * newUser.Speed2)
。 Consequently, among all users with identical speeds, the new user will be inserted as the last one.因此,在所有速度相同的用户中,新用户将作为最后一个插入。
Noteworthy, though, the users
may be not-the-best data structure for this task.但值得注意的是,
users
可能不是此任务的最佳数据结构。
const users = [ { Rank: 1, Speed1: 91, Speed2: 457, Username: 'monizotic', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null }, { Rank: 2, Speed1: 91, Speed2: 457, Username: 'Thachtawan', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null }, { Rank: 3, Speed1: 91, Speed2: 456, Username: 'PassornSibpang', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null }, { Rank: 4, Speed1: 91, Speed2: 456, Username: 'WasinSoikeeree', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null }, { Rank: 5, Speed1: 91, Speed2: 454, Username: 'user1055644', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null } ]; const insertUser = newUser => { const newUserSpeed = newUser.Speed1 * newUser.Speed2; let insertIndex = users.findIndex(user => (user.Speed1 * user.Speed2) < newUserSpeed); insertIndex = insertIndex >= 0? insertIndex: users.length; // assign the new user a rank newUser.Rank = insertIndex + 1; // insert the user users.splice(insertIndex, 0, newUser); // increment ranks of subsequent users users.slice(insertIndex + 1).forEach(user => user.Rank++); }; insertUser({ Rank: null, Speed1: 91, Speed2: 456, Username: 'thaniman', ProfileLink: 'profile_link', VerifiedSpeed: null, Video: null }); console.log(JSON.stringify(users))
It's probably best to do this in stages rather than all at once.最好分阶段进行,而不是一次性完成。
push
the new user object into the data array. push
入数据数组。map
over each user object and calculate the average speed. map
对每个用户 object 并计算平均速度。sort
the returned array by average speed.sort
。map
over the sorted array updating the rank for each user based on the object's position in the array. map
在排序后的数组上根据数组中对象的 position 更新每个用户的排名。 const data=[{Rank:1,Speed1:91,Speed2:457,Username:"monizotic",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null},{Rank:2,Speed1:91,Speed2:457,Username:"Thachtawan",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null},{Rank:3,Speed1:91,Speed2:456,Username:"PassornSibpang",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null},{Rank:4,Speed1:91,Speed2:456,Username:"WasinSoikeeree",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null},{Rank:5,Speed1:91,Speed2:454,Username:"user1055644",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null}]; const newUser={Rank:null,Speed1:91,Speed2:456,Username:"thaniman",ProfileLink:"profile_link",VerifiedSpeed:null,Video:null}; // `push` the new user into the array data.push(newUser); // `map` over the array to create some average speeds const average = data.map(obj => { return {...obj, Avg: (obj.Speed1 + obj.Speed2) / 2 }; }); // `sort` the array by those averages average.sort((a, b) => b.Avg - a.Avg); // `map` over the objects and update the rank // based on the object's position in the array const final = average.map((obj, i) => { const { Rank, Avg,...rest } = obj; return {...rest, Rank: i + 1 }; }); console.log(final);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.