i have trouble with sorting an object in loop. when i do this code i get an Uncaught TypeError: Object # has no method 'sort' . why?) i googled this but doesn't found resolve
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
for (var i = 0; i < responseData.Airlines.length; i++) { function sortPrice() { for (var s = 0; s < responseData.Airlines[i].FaresFull.length; s++) { responseData.Airlines[i].FaresFull[s].Pricing.ADTTotal = parseFloat(responseData.Airlines[i].FaresFull[s].Pricing.ADTTotal); responseData.Airlines[i].FaresFull[s].sort(function (a, b) { return parseFloat(a.Pricing.ADTTotal) > parseFloat(b.Pricing.ADTTotal); }); } } sortPrice(); }
Sounds like you need a table called tblCompany, with an Identifier column (A, B, etc...) and all other info like Name, Address, etc... Then you would make a query that joins your first table to tblCompany, on the Identifier column. Your report would be based off that query, where you could pull in the Name from tblCompany, and all the other info from the first table.
Unless there's something I'm not understanding?
Sort メソッドは Array オブジェクトでのみ使用できます。純粋なオブジェクトを並べ替えようとしていると思います。
見てください:http://www.w3schools.com/jsref/jsref_sort.aspここで彼らは配列をソートします
Sort は配列の関数です。オブジェクトをソートしようとしています。代わりにこれを試してください:
function sortPrice() { for (var i = 0; i < responseData.Airlines.length; i++) { for (var s = 0; s < responseData.Airlines[i].FaresFull.length; s++) { responseData.Airlines[i].FaresFull[s].Pricing.ADTTotal = parseFloat(responseData.Airlines[i].FaresFull[s].Pricing.ADTTotal); } responseData.Airlines[i].FaresFull.sort(function (a, b) { return parseFloat(a.Pricing.ADTTotal) - parseFloat(b.Pricing.ADTTotal); }); } }
リスト全体を並べ替えるのではなく、要素ごとに並べ替えを呼び出そうとしました。内側の for ループから並べ替えを引き出し、コンパレータを -1,0,1 に切り替えます。
何をしたいのか、またはデータがどのように見えるかを教えてくれなかったので、私は大雑把な推測をしました:
// move function header to top function sortPrice(responseData) { for (var i = 0; i < responseData.Airlines.length; i++) { // dereference variables var airline = responseData.Airlines[i]; for (var s = 0; s < airline.FaresFull.length; s++) { var pricing = airline.FaresFull[s].Pricing; pricing.ADTTotal = parseFloat(pricing.ADTTotal); } // move outside the loop airline.FaresFull .sort(function (a, b) { // ^ no [s] - sort the array // don't do parseFloat another time return a.Pricing.ADTTotal - b.Pricing.ADTTotal; }); } } sortPrice(yourData);