2017-10-25 3 views
0

私は、それが持っている投票に基づいてソートしたいオブジェクトを持っていて、新しい/ソートされたオブジェクトにマップします。 ###キーと、それが持っている票の量に基づいてフィルタリングすること -ソート済みネストされたオブジェクトの値に基づいてオブジェクト化されます

const data = { 
    "comment-1508872637211" : { 
    "description" : "Blah", 
    "votes" : 1 
    }, 
    "comment-1508872675949" : { 
    "description" : "Question", 
    "votes" : 11 
    }, 
    "comment-1508898578089" : { 
    "description" : "whatever\n", 
    "votes" : 5 
    }, 
    "comment-1508898637092" : { 
    "description" : "new", 
    "votes" : 15 
    }, 
    "comment-1508900306998" : { 
    "description" : "baj", 
    "votes" : 0 
    } 
} 

console.log(data); 

const sortedOnVotes = Object.entries(data); 

console.log(sortedOnVotes); 

// This is the part I'm getting hung up on 
const newDataObject = sortedOnVotes.map(([key, value]) => value).sort(); 

console.log(newDataObject) 

最終的に私はまだコメントを保持するには、この新しいオブジェクトのために大好きです。たとえばnewDataObjectは、このような何かを返す必要があります:

const newDataObject = { 
      "comment-1508900306998" : { 
      "description" : "baj", 
      "votes" : 0 
      }, 
      "comment-1508872637211" : { 
      "description" : "Blah", 
      "votes" : 1 
      }, 
      "comment-1508898578089" : { 
      "description" : "whatever\n", 
      "votes" : 5 
      }, 
      "comment-1508872675949" : { 
      "description" : "Question", 
      "votes" : 11 
      } 
      "comment-1508898637092" : { 
      "description" : "new", 
      "votes" : 15 
      } 

} 

私はObject.valuesまたはObject.entriesを使用して正しい軌道に乗ってんだけど、私は本当にそれをハングアップなっていると思います。

本当にありがとうございます、ありがとうございます!

https://codepen.io/MathiasaurusRex/pen/RLzYVV

+0

below-ようsort()関数を用いて配列をソートするロジックを記述しますが – brk

+0

使用 'lodashをソートする必要がある場合は、アレイに移動することができます/ sort'、使いやすく、構成が簡単です。ここで[lodash](https://lodash.com/)を参照してください。 – Umesh

答えて

1

あなたは、ソートすることができますjavascriptオブジェクト

const data = { 
    "comment-1508872637211" : { 
    "description" : "Blah", 
    "votes" : 1 
    }, 
    "comment-1508872675949" : { 
    "description" : "Question", 
    "votes" : 11 
    }, 
    "comment-1508898578089" : { 
    "description" : "whatever\n", 
    "votes" : 5 
    }, 
    "comment-1508898637092" : { 
    "description" : "new", 
    "votes" : 15 
    }, 
    "comment-1508900306998" : { 
    "description" : "baj", 
    "votes" : 0 
    } 
} 

console.log(data); 

const sortedOnVotes = Object.entries(data); 

console.log(sortedOnVotes); 

var result = sortedOnVotes.sort(function(a,b) { 
    return a[1].votes - b[1].votes; 
}); 

console.log(result); 
+0

ありがとうございます、これはそれが私のために分類されているように見えます。 元々はキーとオブジェクト値のペアを持つオブジェクトとして元のフォーマットと同じフォーマットに戻すことができるのはまだ不思議です。 答えは配列の配列です。 –

関連する問題