繁体   English   中英

如何根据值将 object 添加到数组中?

[英]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 个用户”。 排名是用户在排行榜中的位置。 每个用户也有Speed1Speed2 Speed1Speed2最高的人排在第一位。 那些最低的人排在最后。 我希望你明白其中的意思。 我需要以某种方式实现一个 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))

最好分阶段进行,而不是一次性完成。

  1. push入数据数组。
  2. map对每个用户 object 并计算平均速度。
  3. 按平均速度对返回的数组进行sort
  4. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM