[英]How to add an object to an array based on its values?
我有一個對象數組,它看起來像這樣:
[{
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
}]
每個 object 是“1 個用戶”。 排名是用戶在排行榜中的位置。 每個用戶也有Speed1和Speed2 。 Speed1和Speed2最高的人排在第一位。 那些最低的人排在最后。 我希望你明白其中的意思。 我需要以某種方式實現一個 function ,它能夠將一個“新用戶”插入到這個帶有對象的數組中。 此類用戶的示例:
{
Rank: null,
Speed1: 91,
Speed2: 456,
Username: 'thaniman',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}
在 object 上面的 Rank 是 null,因為它還不知道。 當此用戶 object 在數組中的正確位置時,它應該會改變
我怎樣才能做到這一點?
請參閱以下具有O(N)
時間復雜度的解決方案。
假設所需的插入索引邏輯是(user.Speed1 * user.Speed2) < (newUser.Speed1 * newUser.Speed2)
。 因此,在所有速度相同的用戶中,新用戶將作為最后一個插入。
但值得注意的是, 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))
最好分階段進行,而不是一次性完成。
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.