使用javascript比较数组中的对象列表

时间:2017-05-30 12:39:43

标签: javascript

如何使用javascript比较数组中对象的列表 这是一个例子

"listArry": [
 {
  "date": "2017-05-29T11:59:34.363Z",
  "status": "CLOSED",
  "name":"sam1"
},
{
  "date": "2017-05-22T11:59:34.363Z",
  "status": "CLOSED",
  "name":"sam2"
},
{
  "date": "2017-05-21T11:59:34.363Z",
  "status": "CLOSED",
  "name":"sam3"
},
{
  "date": "2017-05-30T11:59:34.363Z",
  "status": "OPEN",
  "name":"sam"
}

由此,我只需要获得new date()个对象

我的输出将是

{
  "date": "2017-05-30T11:59:34.363Z",
  "status": "OPEN",
  "name":"sam"
}

3 个答案:

答案 0 :(得分:0)

您可以使用lodash库,特别是find方法,根据您的条件选择项目:lodash-find()

答案 1 :(得分:0)

您可以只比较给定日期并获取更新的对象。



var listArray = [{ date: "2017-05-29T11:59:34.363Z", status: "CLOSED", name: "sam1" }, { date: "2017-05-22T11:59:34.363Z", status: "CLOSED", name: "sam2" }, { date: "2017-05-21T11:59:34.363Z", status: "CLOSED", name: "sam3" }, { date: "2017-05-30T11:59:34.363Z", status: "OPEN", name: "sam" }],
    result = listArray.reduce(function(a, b) {
        return a > b ? a : b;
    });

console.log(result);




答案 2 :(得分:0)

更强大的方法是命令他们并选择第一个

time_T