在对象中查找后一个日期

时间:2018-02-16 17:39:30

标签: javascript

我需要在对象中迭代特定对象,然后找到具有后一个Date的对象。

这是我的对象的例子:

var o = {
  "data": [
    {
      "id": 2,
      "category": "test1",
      "parents": [
        {
          "id": 31,
          "children": [
            {
              "firstName": "Steve",
              "lastName": "Martin",
              "created": "2018-04-06T22:00:00.000Z"
            },
            {
              "firstName": "Steve2",
              "lastName": "Martin2",
              "created": "2016-02-10T23:00:00.000Z"
            }
          ]
        },
        {
          "id": 31,
          "children": [
            {
              "firstName": "Julia",
              "lastName": "Robbery",
              "created": "2015-01-06T23:00:00.000Z"
            },
            {
              "firstName": "Nikol",
              "lastName": "Surachenko",
              "created": "2017-04-06T22:00:00.000Z"
            },
            {
              "firstName": "Nikol",
              "lastName": "Surachenko",
              "created": "2011-06-05T22:00:00.000Z"
            }
          ]
        }
      ]
    }
  ]
}

我试过了:

var latter = null;
for (var i = 0; i < o.data[0].parents.length; i++) {
    for (var j = 0; j < o.data[0].parents[i].children.length; j++) {
        if (latter == null || moment(latter) < moment(o.data[0].parents[i].children[j].created))
            latter=o.data[0].parents[i].children[j].created;
    }
}

你能否告诉我是否存在更漂亮的方式?比如用lambda等? 谢谢你的建议。

3 个答案:

答案 0 :(得分:2)

“漂亮”是主观的,但在我看来,与lodash你可以用更清洁的方式写它:

mostRecent = _.max(
    _.flatMap(
        _.get(o, 'data.0.parents'),
        'children'),
    'created')

如果不能选择lodash,您可以推出自己的临时微框架:

let get = p => o => o[p];
let flatMap = (a, f) => [].concat(...a.map(f));
let max = (a, f) => a.map(x => [x, f(x)]).reduce((m, p) => m[1] > p[1] ? m : p)[0];

mostRecent = max(
    flatMap(o.data[0].parents, get('children')),
    get('created')
)

答案 1 :(得分:1)

使用for-loops并比较日期。

这种方法下游到整个对象中以获得正确的对象。

var o = {   "data": [{     "id": 2,     "category": "test1",     "parents": [{         "id": 31,         "children": [{             "firstName": "Steve",             "lastName": "Martin",             "created": "2018-04-06T22:00:00.000Z"           },           {             "firstName": "Steve2",             "lastName": "Martin2",             "created": "2016-02-10T23:00:00.000Z"           }         ]       },       {         "id": 31,         "children": [{             "firstName": "Julia",             "lastName": "Robbery",             "created": "2015-01-06T23:00:00.000Z"           },           {             "firstName": "Nikol",             "lastName": "Surachenko",             "created": "2017-04-06T22:00:00.000Z"           },           {             "firstName": "Nikol",             "lastName": "Surachenko",             "created": "2011-06-05T22:00:00.000Z"           }         ]       }     ]   }] }

var result = {};
for (var obj of o.data) {
  for (var p of obj.parents) {
    for (var c of p.children) {
      result = !result.created || Date.parse(c.created) > Date.parse(result.created) ? c : result;
    }
  }
}

console.log(result);

答案 2 :(得分:0)

你可能会更有用:

 const result = o.data[0].parents.reduce((res, {children}) => res.concat(children), [])
  .reduce((old, curr) => Date(old.created) > Date(curr.created) ? old : curr);