【问题标题】:Adding two json objects and sorting添加两个json对象并排序
【发布时间】:2012-01-15 10:49:47
【问题描述】:

我有三个类似的 json 对象。现在我希望加入整个列表,然后根据索引之一对完整列表进行排序。这里是对象描述

对象 1

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}]

对象 2

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}] 

对象 3

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}] 

一旦我加入所有这些,我希望将完整的数组(新数组)w.r.t 排序为价格指数。

对此的任何提示将不胜感激。 谢谢:)

【问题讨论】:

  • 三个对象的数据不同
  • 你卡在哪里了? How to parse JSONhow to concatenate arrayshow to sort an array of objects 还是别的什么?
  • 我无法连接它们
  • 然后点击我评论中的链接。仅供参考,如果您已经将 JSON 解析为 JavaScript 对象/数组,那么您的问题不再与 JSON 相关。
  • 什么更可取,连接 JSON 对象或 javascript 对象?

标签: javascript json


【解决方案1】:

如果 Object1 、 Object2 和 Object3 是 JSON 字符串,则使用 eval 函数将其转换为 Javascript 对象。

然后使用concat 方法合并它们。 http://www.w3schools.com/jsref/jsref_concat_array.asp

var mergedArray = arr1.concat(arr2, arr3);

然后在 Javascript 数组中使用sort 方法进行排序。 参考:http://www.w3schools.com/jsref/jsref_sort.asp 参考:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

var sorted = mergedArray.sort(function(a, b){

    // This function is used by sort method for sorting process
    // This function gets called with parameters a,b which are the elements in array (here product objects from your JSON)
    // if this function returns value < 0 a is placed before b
    // if this function returns 0 nothing is changed
    // if this function returns value > 0 b is placed before a
    // a.price.replace("Rs.", "") removes Rs. from price string. so "Rs. 200" becomes 200
    // parseFloat(a.price.replace("Rs.", "")) makes it number.  "200" becomes 200. "200.50" becomes 200.5
    // priceA - priceB returns -ve value if priceA < priceB, 0 if priceA = priceB, +ve value if priceA > priceB.

    var priceA = parseFloat(a.price.replace("Rs.", ""));
    var priceB = parseFloat(b.price.replace("Rs.", ""));
    return priceA - priceB;



});

使用return priceB - priceA; 进行降序排列。 jsfiddle:http://jsfiddle.net/diode/FzzHz/

.

【讨论】:

  • 你能解释一下最后一部分吗?合并数组后,我没有得到这段代码如何对它们进行排序。请解释一下
  • 非常感谢。我得到了一切:D
【解决方案2】:

将它们转换为对象,this 应该可以解决问题

【讨论】:

    【解决方案3】:

    您可以使用concat 方法连接数组:

    var result = arr1.concat(arr2, arr3);
    

    然后您可以对结果数组进行排序。

    让我们编写排序函数,使用prod 属性对它们进行排序(您可以按您希望的任何属性对它们进行排序):

    function SortByProd(a, b) {
        var aProd = a.prod.toLowerCase();
        var bProd = b.prod.toLowerCase(); 
        return ((aProd < bProd) ? -1 : ((aProd > bProd) ? 1 : 0));
    }
    

    然后排序:

    result.sort(SortByProd);
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 1970-01-01
      • 2021-09-17
      • 2018-01-14
      • 2021-04-17
      • 2019-01-16
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多