19.01.2018, 12:39
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Сообщение от рони
|
Cache,
сделайте ваш обьект для примера, ключей на 10
|
во пример, массива, который используется сейчас:
arr=[{"id":31061,"v1":21,"v2":3,"v3":25,"v4":337,"v5":70,"v6":0,"v7":0,"v8":0,"v9":8,"v10":17,"v11":70,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4659,"v1":14,"v2":2,"v3":397,"v4":473,"v5":58,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":58,"v13":0,"v14":0,"v15":0,"v16":0},{"id":34501,"v1":12,"v2":2,"v3":122,"v4":401,"v5":54,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":24,"v13":0,"v14":0,"v15":0,"v16":0},{"id":33700,"v1":23,"v2":3,"v3":31,"v4":332,"v5":52,"v6":0,"v7":0,"v8":0,"v9":0,"v10":45,"v11":27,"v12":24,"v13":10,"v14":0,"v15":0,"v16":0},{"id":33465,"v1":11,"v2":2,"v3":340,"v4":416,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":47,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35485,"v1":10,"v2":2,"v3":97,"v4":516,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":37,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4420,"v1":22,"v2":3,"v3":25,"v4":319,"v5":45,"v6":3.75,"v7":18,"v8":0,"v9":10,"v10":0,"v11":45,"v12":0,"v13":0,"v14":15,"v15":0,"v16":0},{"id":2624,"v1":20,"v2":3,"v3":25,"v4":304,"v5":44,"v6":0,"v7":0,"v8":0,"v9":0,"v10":6,"v11":21,"v12":23,"v13":0,"v14":0,"v15":0,"v16":0},{"id":3888,"v1":10,"v2":2,"v3":370,"v4":446,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":41,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":31505,"v1":8,"v2":2,"v3":103,"v4":516,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":16,"v12":25,"v13":0,"v14":0,"v15":0,"v16":0},{"id":9148,"v1":27,"v2":3,"v3":99,"v4":286,"v5":39.3,"v6":3.75,"v7":0,"v8":0,"v9":0,"v10":0,"v11":15,"v12":18,"v13":63,"v14":15,"v15":0,"v16":0},{"id":8814,"v1":13,"v2":2,"v3":77,"v4":461,"v5":38.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":22,"v14":0,"v15":0,"v16":0},{"id":9863,"v1":7,"v2":2,"v3":111,"v4":461,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":31,"v12":5,"v13":0,"v14":0,"v15":0,"v16":0},{"id":11403,"v1":8,"v2":2,"v3":39,"v4":374,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":0,"v14":0,"v15":0,"v16":0},{"id":33283,"v1":11,"v2":2,"v3":8,"v4":434,"v5":32.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":30,"v13":22,"v14":0,"v15":0,"v16":0},{"id":2654,"v1":23,"v2":3,"v3":58,"v4":281,"v5":31,"v6":16.5,"v7":0,"v8":0,"v9":0,"v10":12,"v11":12,"v12":15,"v13":40,"v14":0,"v15":33,"v16":0},{"id":5697,"v1":11,"v2":2,"v3":35,"v4":445,"v5":30,"v6":0,"v7":0,"v8":0,"v9":18,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":10291,"v1":8,"v2":2,"v3":89,"v4":507,"v5":30,"v6":5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":10,"v16":0},{"id":36762,"v1":10,"v2":2,"v3":191,"v4":539,"v5":30,"v6":12,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":0,"v16":12},{"id":2205,"v1":13,"v2":2,"v3":172,"v4":375,"v5":27.6,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":24,"v13":36,"v14":0,"v15":0,"v16":0},{"id":2521,"v1":11,"v2":2,"v3":173,"v4":437,"v5":27.4,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":24,"v14":0,"v15":0,"v16":0},{"id":6023,"v1":12,"v2":2,"v3":120,"v4":355,"v5":27,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":24,"v12":0,"v13":30,"v14":0,"v15":0,"v16":0},{"id":31237,"v1":10,"v2":2,"v3":167,"v4":387,"v5":25,"v6":20,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":0,"v14":0,"v15":0,"v16":20},{"id":32598,"v1":10,"v2":2,"v3":70,"v4":366,"v5":25,"v6":5.5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":25,"v12":0,"v13":0,"v14":22,"v15":0,"v16":0},{"id":37239,"v1":7,"v2":2,"v3":198,"v4":471,"v5":25,"v6":2.5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":0,"v14":10,"v15":0,"v16":0},{"id":9003,"v1":10,"v2":2,"v3":143,"v4":390,"v5":24,"v6":0,"v7":20,"v8":0,"v9":0,"v10":0,"v11":24,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":37116,"v1":13,"v2":2,"v3":120,"v4":408,"v5":24,"v6":0,"v7":0,"v8":0,"v9":30,"v10":0,"v11":24,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4939,"v1":13,"v2":2,"v3":79,"v4":354,"v5":22,"v6":0,"v7":0,"v8":0,"v9":0,"v10":36,"v11":22,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":9838,"v1":11,"v2":2,"v3":37,"v4":509,"v5":22,"v6":30,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":22,"v13":0,"v14":0,"v15":0,"v16":30},{"id":30922,"v1":8,"v2":2,"v3":177,"v4":518,"v5":22,"v6":0,"v7":20,"v8":0,"v9":0,"v10":0,"v11":12,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35830,"v1":11,"v2":2,"v3":222,"v4":461,"v5":22,"v6":0,"v7":0,"v8":25,"v9":0,"v10":0,"v11":0,"v12":22,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35852,"v1":4,"v2":1,"v3":65,"v4":651,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36298,"v1":4,"v2":1,"v3":127,"v4":700,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36578,"v1":4,"v2":1,"v3":87,"v4":506,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36697,"v1":4,"v2":1,"v3":405,"v4":660,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":7970,"v1":9,"v2":2,"v3":36,"v4":462,"v5":18,"v6":0,"v7":0,"v8":25,"v9":0,"v10":0,"v11":6,"v12":12,"v13":0,"v14":0,"v15":0,"v16":0}}]
сорри, спойлера не нашел
|
|
19.01.2018, 12:40
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Сообщение от рони
|
а зачем, если можно решить через массив?
|
чтобы можно было обратиться к объекту по его id
например так:
obj[id]
|
|
19.01.2018, 12:43
|
|
Тлен
|
|
Регистрация: 02.01.2010
Сообщений: 6,587
|
|
arr=[{"id":31061,"v1":21,"v2":3,"v3":25,"v4":337,"v5":70,"v6":0,"v7":0,"v8":0,"v9":8,"v10":17,"v11":70,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4659,"v1":14,"v2":2,"v3":397,"v4":473,"v5":58,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":58,"v13":0,"v14":0,"v15":0,"v16":0},{"id":34501,"v1":12,"v2":2,"v3":122,"v4":401,"v5":54,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":24,"v13":0,"v14":0,"v15":0,"v16":0},{"id":33700,"v1":23,"v2":3,"v3":31,"v4":332,"v5":52,"v6":0,"v7":0,"v8":0,"v9":0,"v10":45,"v11":27,"v12":24,"v13":10,"v14":0,"v15":0,"v16":0},{"id":33465,"v1":11,"v2":2,"v3":340,"v4":416,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":47,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35485,"v1":10,"v2":2,"v3":97,"v4":516,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":37,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4420,"v1":22,"v2":3,"v3":25,"v4":319,"v5":45,"v6":3.75,"v7":18,"v8":0,"v9":10,"v10":0,"v11":45,"v12":0,"v13":0,"v14":15,"v15":0,"v16":0},{"id":2624,"v1":20,"v2":3,"v3":25,"v4":304,"v5":44,"v6":0,"v7":0,"v8":0,"v9":0,"v10":6,"v11":21,"v12":23,"v13":0,"v14":0,"v15":0,"v16":0},{"id":3888,"v1":10,"v2":2,"v3":370,"v4":446,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":41,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":31505,"v1":8,"v2":2,"v3":103,"v4":516,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":16,"v12":25,"v13":0,"v14":0,"v15":0,"v16":0},{"id":9148,"v1":27,"v2":3,"v3":99,"v4":286,"v5":39.3,"v6":3.75,"v7":0,"v8":0,"v9":0,"v10":0,"v11":15,"v12":18,"v13":63,"v14":15,"v15":0,"v16":0},{"id":8814,"v1":13,"v2":2,"v3":77,"v4":461,"v5":38.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":22,"v14":0,"v15":0,"v16":0},{"id":9863,"v1":7,"v2":2,"v3":111,"v4":461,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":31,"v12":5,"v13":0,"v14":0,"v15":0,"v16":0},{"id":11403,"v1":8,"v2":2,"v3":39,"v4":374,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":0,"v14":0,"v15":0,"v16":0},{"id":33283,"v1":11,"v2":2,"v3":8,"v4":434,"v5":32.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":30,"v13":22,"v14":0,"v15":0,"v16":0},{"id":2654,"v1":23,"v2":3,"v3":58,"v4":281,"v5":31,"v6":16.5,"v7":0,"v8":0,"v9":0,"v10":12,"v11":12,"v12":15,"v13":40,"v14":0,"v15":33,"v16":0},{"id":5697,"v1":11,"v2":2,"v3":35,"v4":445,"v5":30,"v6":0,"v7":0,"v8":0,"v9":18,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":10291,"v1":8,"v2":2,"v3":89,"v4":507,"v5":30,"v6":5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":10,"v16":0},{"id":36762,"v1":10,"v2":2,"v3":191,"v4":539,"v5":30,"v6":12,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":0,"v13":0,"v14":0,"v15":0,"v16":12},{"id":2205,"v1":13,"v2":2,"v3":172,"v4":375,"v5":27.6,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":24,"v13":36,"v14":0,"v15":0,"v16":0},{"id":2521,"v1":11,"v2":2,"v3":173,"v4":437,"v5":27.4,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":24,"v14":0,"v15":0,"v16":0},{"id":6023,"v1":12,"v2":2,"v3":120,"v4":355,"v5":27,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":24,"v12":0,"v13":30,"v14":0,"v15":0,"v16":0},{"id":31237,"v1":10,"v2":2,"v3":167,"v4":387,"v5":25,"v6":20,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":0,"v14":0,"v15":0,"v16":20},{"id":32598,"v1":10,"v2":2,"v3":70,"v4":366,"v5":25,"v6":5.5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":25,"v12":0,"v13":0,"v14":22,"v15":0,"v16":0},{"id":37239,"v1":7,"v2":2,"v3":198,"v4":471,"v5":25,"v6":2.5,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":25,"v13":0,"v14":10,"v15":0,"v16":0},{"id":9003,"v1":10,"v2":2,"v3":143,"v4":390,"v5":24,"v6":0,"v7":20,"v8":0,"v9":0,"v10":0,"v11":24,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":37116,"v1":13,"v2":2,"v3":120,"v4":408,"v5":24,"v6":0,"v7":0,"v8":0,"v9":30,"v10":0,"v11":24,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":4939,"v1":13,"v2":2,"v3":79,"v4":354,"v5":22,"v6":0,"v7":0,"v8":0,"v9":0,"v10":36,"v11":22,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":9838,"v1":11,"v2":2,"v3":37,"v4":509,"v5":22,"v6":30,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":22,"v13":0,"v14":0,"v15":0,"v16":30},{"id":30922,"v1":8,"v2":2,"v3":177,"v4":518,"v5":22,"v6":0,"v7":20,"v8":0,"v9":0,"v10":0,"v11":12,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35830,"v1":11,"v2":2,"v3":222,"v4":461,"v5":22,"v6":0,"v7":0,"v8":25,"v9":0,"v10":0,"v11":0,"v12":22,"v13":0,"v14":0,"v15":0,"v16":0},{"id":35852,"v1":4,"v2":1,"v3":65,"v4":651,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36298,"v1":4,"v2":1,"v3":127,"v4":700,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36578,"v1":4,"v2":1,"v3":87,"v4":506,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":36697,"v1":4,"v2":1,"v3":405,"v4":660,"v5":18,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":18,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},{"id":7970,"v1":9,"v2":2,"v3":36,"v4":462,"v5":18,"v6":0,"v7":0,"v8":25,"v9":0,"v10":0,"v11":6,"v12":12,"v13":0,"v14":0,"v15":0,"v16":0}];
var result = arr
.sort(function(a, b){
return b.v2 - a.v2
}) //сортируем по убыванию v2
.slice(0, 20) //отрезаем верхние 20
.reduce(function(a, b) {
return a[b.id] = b, a;
}, {}); //набиваем новый объект, где ключами - id
alert(JSON.stringify(result,0,'\t'));
Как видно, независимо от добавления порядок ключей в новом объекте отсортирован по возрастанию и с этим ничего не сделать ибо стандарт. Так что либо удобное обращение по result[id] либо нужный порядок пр итерации.
__________________
29375, 35
Последний раз редактировалось Aetae, 19.01.2018 в 12:51.
|
|
19.01.2018, 12:45
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Сообщение от рони
|
Тогда напиши что "дано" и что, таки нужно получить в итоге
|
что дано, я написал в первом сообщении, т.е. вот этот мсассив, что процитировал выше (это его малая часть) я хочу переделать в объект
но при этом не иметь геммороя с получением отсортированных данных, не гоняя весь объект через преобразование в массив и потом сортировку
|
|
19.01.2018, 12:46
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Aetae,
я еще раз повторяю, у меня есть функция сортировки этого массива
мне не надо сортировать ЭТОТ массив, что вы уже второй раз пытаетесь сделать, я хочу преобразовать его в объект (пример в первом посте) но при этом иметь возможность получить из него нужные отсортированные данные
|
|
19.01.2018, 12:50
|
|
Профессор
|
|
Регистрация: 27.05.2010
Сообщений: 33,112
|
|
я пас ...
|
|
19.01.2018, 12:54
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Aetae,
вот объект который я хочу иметь в итоге:
obj={
31061:{"id":31061,"v1":21,"v2":3,"v3":25,"v4":337,"v5":70,"v6":0,"v7":0,"v8":0,"v9":8,"v10":17,"v11":70,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
4659:{"id":4659,"v1":14,"v2":2,"v3":397,"v4":473,"v5":58,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":58,"v13":0,"v14":0,"v15":0,"v16":0},
34501:{"id":34501,"v1":12,"v2":2,"v3":122,"v4":401,"v5":54,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":24,"v13":0,"v14":0,"v15":0,"v16":0},
33700:{"id":33700,"v1":23,"v2":3,"v3":31,"v4":332,"v5":52,"v6":0,"v7":0,"v8":0,"v9":0,"v10":45,"v11":27,"v12":24,"v13":10,"v14":0,"v15":0,"v16":0},
33465:{"id":33465,"v1":11,"v2":2,"v3":340,"v4":416,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":47,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
35485:{"id":35485,"v1":10,"v2":2,"v3":97,"v4":516,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":37,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},
4420:{"id":4420,"v1":22,"v2":3,"v3":25,"v4":319,"v5":45,"v6":3.75,"v7":18,"v8":0,"v9":10,"v10":0,"v11":45,"v12":0,"v13":0,"v14":15,"v15":0,"v16":0},
2624:{"id":2624,"v1":20,"v2":3,"v3":25,"v4":304,"v5":44,"v6":0,"v7":0,"v8":0,"v9":0,"v10":6,"v11":21,"v12":23,"v13":0,"v14":0,"v15":0,"v16":0},
3888:{"id":3888,"v1":10,"v2":2,"v3":370,"v4":446,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":41,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
31505:{"id":31505,"v1":8,"v2":2,"v3":103,"v4":516,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":16,"v12":25,"v13":0,"v14":0,"v15":0,"v16":0},
9148:{"id":9148,"v1":27,"v2":3,"v3":99,"v4":286,"v5":39.3,"v6":3.75,"v7":0,"v8":0,"v9":0,"v10":0,"v11":15,"v12":18,"v13":63,"v14":15,"v15":0,"v16":0},
8814:{"id":8814,"v1":13,"v2":2,"v3":77,"v4":461,"v5":38.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":22,"v14":0,"v15":0,"v16":0},
9863:{"id":9863,"v1":7,"v2":2,"v3":111,"v4":461,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":31,"v12":5,"v13":0,"v14":0,"v15":0,"v16":0},
11403:{"id":11403,"v1":8,"v2":2,"v3":39,"v4":374,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":0,"v14":0,"v15":0,"v16":0}
}
и из него извлекать отсортированные по определенному ключу данные
|
|
19.01.2018, 12:55
|
|
Тлен
|
|
Регистрация: 02.01.2010
Сообщений: 6,587
|
|
Cache, объект и сортировка несовместимы. Ты не властен над порядком ключей объекта. Так или иначе тебе придётся пердолить объект обратно в массив, сортировать массив, и пердолить результат обратно в объект:
obj={
31061:{"id":31061,"v1":21,"v2":3,"v3":25,"v4":337,"v5":70,"v6":0,"v7":0,"v8":0,"v9":8,"v10":17,"v11":70,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
4659:{"id":4659,"v1":14,"v2":2,"v3":397,"v4":473,"v5":58,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":58,"v13":0,"v14":0,"v15":0,"v16":0},
34501:{"id":34501,"v1":12,"v2":2,"v3":122,"v4":401,"v5":54,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":30,"v12":24,"v13":0,"v14":0,"v15":0,"v16":0},
33700:{"id":33700,"v1":23,"v2":3,"v3":31,"v4":332,"v5":52,"v6":0,"v7":0,"v8":0,"v9":0,"v10":45,"v11":27,"v12":24,"v13":10,"v14":0,"v15":0,"v16":0},
33465:{"id":33465,"v1":11,"v2":2,"v3":340,"v4":416,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":47,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
35485:{"id":35485,"v1":10,"v2":2,"v3":97,"v4":516,"v5":47,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":37,"v12":10,"v13":0,"v14":0,"v15":0,"v16":0},
4420:{"id":4420,"v1":22,"v2":3,"v3":25,"v4":319,"v5":45,"v6":3.75,"v7":18,"v8":0,"v9":10,"v10":0,"v11":45,"v12":0,"v13":0,"v14":15,"v15":0,"v16":0},
2624:{"id":2624,"v1":20,"v2":3,"v3":25,"v4":304,"v5":44,"v6":0,"v7":0,"v8":0,"v9":0,"v10":6,"v11":21,"v12":23,"v13":0,"v14":0,"v15":0,"v16":0},
3888:{"id":3888,"v1":10,"v2":2,"v3":370,"v4":446,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":41,"v12":0,"v13":0,"v14":0,"v15":0,"v16":0},
31505:{"id":31505,"v1":8,"v2":2,"v3":103,"v4":516,"v5":41,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":16,"v12":25,"v13":0,"v14":0,"v15":0,"v16":0},
9148:{"id":9148,"v1":27,"v2":3,"v3":99,"v4":286,"v5":39.3,"v6":3.75,"v7":0,"v8":0,"v9":0,"v10":0,"v11":15,"v12":18,"v13":63,"v14":15,"v15":0,"v16":0},
8814:{"id":8814,"v1":13,"v2":2,"v3":77,"v4":461,"v5":38.2,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":22,"v14":0,"v15":0,"v16":0},
9863:{"id":9863,"v1":7,"v2":2,"v3":111,"v4":461,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":31,"v12":5,"v13":0,"v14":0,"v15":0,"v16":0},
11403:{"id":11403,"v1":8,"v2":2,"v3":39,"v4":374,"v5":36,"v6":0,"v7":0,"v8":0,"v9":0,"v10":0,"v11":0,"v12":36,"v13":0,"v14":0,"v15":0,"v16":0}
};
var result = Object.values(obj) //получаем массив значений из объекта
.sort(function(a, b){
return b.v2 - a.v2
}) //сортируем по убыванию v2
.slice(0, 20) //отрезаем верхние 20
.reduce(function(a, b) {
return a[b.id] = b, a;
}, {}); //набиваем новый объект, где ключами - id
alert(JSON.stringify(result,0,'\t'));
__________________
29375, 35
Последний раз редактировалось Aetae, 19.01.2018 в 13:05.
|
|
19.01.2018, 13:07
|
Кандидат Javascript-наук
|
|
Регистрация: 02.05.2013
Сообщений: 111
|
|
Сообщение от Aetae
|
var result = arr
.sort(function(a, b){
return b.v2 - a.v2
}) //сортируем по убыванию v2
.slice(0, 20) //отрезаем верхние 20
.reduce(function(a, b) {
return a[b.id] = b, a;
}, {}); //набиваем новый объект, где ключами - id
alert(JSON.stringify(result,0,'\t'));
Как видно, независимо от добавления порядок ключей в новом объекте отсортирован по возрастанию и с этим ничего не сделать ибо стандарт. Так что либо удобное обращение по result[id] либо нужный порядок пр итерации.
|
если я правильно понял, вы предлагаете хранить как массив, в том виде что есть сейчас. так и объект для обращения по ключу?
не оптимально, мне кажется, будет занимать в памяти в 2 раза больше места, при том что вложенных объектов около 300 шт и может быть больше
к тому же .slice(0, 20) в данном случае не даст поместить в туда все данные только часть разве нет?
|
|
19.01.2018, 13:10
|
|
Тлен
|
|
Регистрация: 02.01.2010
Сообщений: 6,587
|
|
20 - это из условия конкретной задачи. По остальному см. выше.
__________________
29375, 35
|
|
|
|