解析来自服务器的JSON并重新排序

时间:2018-04-19 02:57:16

标签: javascript jquery json

我正在调用一个API,它正在输出一个JSON(见下文)。我可以通过使用AJAX进行解析。想知道我怎么能......

  1. 根据日期(最新的第一个)重新排序数据,然后
  2. 仅显示前三个
  3. JSON输出

    {
        "beds": {
            "casper": {
                "name": "angel",
                "description": "lorem ipsum",
                "orderDate": "09/20/2018"
            },
            "tuft": {
                "name": "relax",
                "description": "lorem ipsum",
                "orderDate": "05/12/2018"
            },
            "saatva": {
                "name": "heaven",
                "description": "lorem ipsum",
                "orderDate": "07/03/2018"
            },
            "dream": {
                "name": "sweet",
                "description": "lorem ipsum",
                "orderDate": "04/28/2018"
            },
            "rocky": {
                "name": "painful",
                "description": "lorem ipsum",
                "orderDate": "02/15/2018"
            }
        }
    }
    

    这是AJAX成功后的代码

    $.ajax({
        ...
        ...
        ...
        success: function(result) {
    
            $('.container-for-beds').each(function(i) {
    
                $(this).prepend(
                    '<div class="bedBrand"><p>' + result.beds[i].name + '</p><p>' + result.beds[i].description + '</p><p>' + result.beds[i].orderDate + '</p></div>'
                );
    
            });
    
        }
    });
    

    如果一切顺利,HTML应该看起来像......

    <div class="container-for-beds">
        <div class="bedBrand">
            <p>heaven by saatva</p>
            <p>lorem ipsum</p>
            <p>07/03/2018</p>
        </div>
        <div class="bedBrand">
            <p>relax by tuft</p>
            <p>lorem ipsum</p>
            <p>05/12/2018</p>
        </div>
        <div class="bedBrand">
            <p>angel by casper</p>
            <p>lorem ipsum</p>
            <p>04/20/2018</p>
        </div>
    </div>
    

    非常感谢您的见解!

1 个答案:

答案 0 :(得分:1)

  

我可以使用AJAX解析

ajax的目标是进行异步调用,它可能不会parsing

其次,不保证js对象中键的顺序。话虽如此,您可以从此对象创建一个新数组,并根据日期对数组进行排序。使用slice方法获取前三个对象

&#13;
&#13;
var data = {
  "beds": {
    "casper": {
      "name": "angel",
      "description": "lorem ipsum",
      "orderDate": "09/20/2018"
    },
    "tuft": {
      "name": "relax",
      "description": "lorem ipsum",
      "orderDate": "05/12/2018"
    },
    "saatva": {
      "name": "heaven",
      "description": "lorem ipsum",
      "orderDate": "07/03/2018"
    },
    "dream": {
      "name": "sweet",
      "description": "lorem ipsum",
      "orderDate": "04/28/2018"
    },
    "rocky": {
      "name": "painful",
      "description": "lorem ipsum",
      "orderDate": "02/15/2018"
    }
  }
};
var createArray = [];
// looping the object using for..in
for (keys in data.beds) {
  var currentKey = data.beds[keys];
  // adding the nested object key name as a property 
  currentKey.parent = keys;
   // {"name": "angel","description": "lorem ipsum","orderDate": "09/20/2018","parent": "casper"}
  createArray.push(currentKey)
}
var sortedByDate = createArray.sort(function(a, b) {
  // sorting based on date, it will sort in ascending order
  //change to new Date(b.orderDate) - new Date(b.orderDate)
  // to sort in descending order
  return new Date(a.orderDate) - new Date(b.orderDate)
}).slice(0,3)
console.log(sortedByDate)
&#13;
&#13;
&#13;