简体   繁体   English

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

[英]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 .每个用户也有Speed1Speed2 Whoever has the highest Speed1 and Speed2 is in the first place. Speed1Speed2最高的人排在第一位。 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.最好分阶段进行,而不是一次性完成。

  1. push the new user object into the data array. push入数据数组。
  2. map over each user object and calculate the average speed. map对每个用户 object 并计算平均速度。
  3. sort the returned array by average speed.按平均速度对返回的数组进行sort
  4. 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.

相关问题 如何根据条件添加对象数组的值? JS - How to Add the values ​of an object array based on a condition? JS 如何对数组进行重复数据删除,并根据其重复向数组的每个 object 添加其他属性 - How can I de-dupe an array, and add additional properties to each object of the array based on its duplications 如何基于Javascript中的对象键从对象数组添加对象值 - How to add object values from an object array based on an object key in Javascript 如何将数组值添加到对象 - How to add array values to an object 根据 Firestore 上的值从数组中删除 object(地图) - Removing an object (map) from array based on its values on firestore 将值添加到具有基于数组的键深度的对象 - Add a value to an object with its key depth based on an array 如何在第一次单击时将 object 添加到数组中,并在之后每次单击时更新其中一个值 - How to add an object to an array on the first click and update one of its values with every click made afterwards 如何根据条件在数组中添加对象元素 - How to add object element in array based on condition 如何将数据映射到具有特定字段和值的新数组,并根据日期范围每天添加一个对象 - How to map data to a new array with specific fields and values and add a single object per day based on a range of dates 将数组值转换为对象,根据索引位置添加键 - convert array values to object, add keys based on index position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM