A
A
Artem2020-04-15 11:48:50
JavaScript
Artem, 2020-04-15 11:48:50

How can I list the variables in the objects with the largest value?

I have json file how can i output something like this list obj2, value:3 obj3, value:2 obj1, value:0
{obj1:{value:0},obj2:{value:3},obj3:{value:2},...}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-15
@Kp18

json = {obj1:{value:0},obj2:{value:3},obj3:{value:2}}

Object.entries(json)   // объект в массив
  .map(( [key, val] ) => [key, val.value])  // убираем лишнее
  .sort((a, b) => b[1] - a[1])  // сортируем
  .map(a => `${a[0]}, value:${a[1]}`)  // переводим в строки
  .join('\n')  // соединяем

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question