我有一個物件陣列,它看起來像這樣:
[{
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
}]
每個物件是“1 個用戶”。排名是用戶在排行榜中的位置。每個用戶也有Speed1和Speed2。Speed1和Speed2最高的人排在第一位。那些最低的人排在最后。我希望你明白其中的意思。我需要以某種方式實作一個函式,該函式能夠將“新用戶”插入到這個帶有物件的陣列中。此類用戶的示例:
{
Rank: null,
Speed1: 91,
Speed2: 456,
Username: 'thaniman',
ProfileLink: 'profile_link',
VerifiedSpeed: null,
Video: null
}
在上面的物件中,Rank 是空的,因為它還不知道。當這個用戶物件在陣列中的正確位置時它應該改變
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
請參閱以下具有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))
uj5u.com熱心網友回復:
最好分階段進行,而不是一次性完成。
push新的用戶物件到資料陣列中。map在每個用戶物件上計算平均速度。sort按平均速度回傳的陣列。map在排序后的陣列上根據物件在陣列中的位置更新每個用戶的排名。
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497759.html
標籤:javascript 数组 javascript 对象
上一篇:keys()、values()、entry()和Symbol.iterator有什么區別?
下一篇:選擇無線電輸入時如何輸入值?
