过滤一个对象JS

时间:2016-09-28 13:04:09

标签: javascript reactjs ecmascript-6 redux react-redux

我正在尝试过滤掉一个对象,但不起作用....

这是代码:

let obj = Object.assign({},[{
        "_id": 1,
        "fecha": "2016-09-28T20:49:00.000Z"
    }, {
        "_id": 2,
        "fecha": "2016-09-26T20:48:00.000Z"
    }, {
        "_id": 3,
        "fecha": "2016-09-26T21:04:00.000Z"
    }, {
        "_id": 4,
        "fecha": "2016-09-23T21:02:00.000Z"
    }, {
        "_id": 14,
        "fecha": "2016-09-24T21:02:00.000Z"
    }, {
        "_id": 15,
        "fecha": "2016-09-24T21:01:00.000Z"
    }, {
        "_id": 17,
        "fecha": "2016-09-23T19:04:00.000Z"
    }, {
        "_id": 18,
        "fecha": "2016-09-23T19:24:00.000Z"
    }, {
        "_id": 23,
        "fecha": "2016-09-23T21:04:00.000Z"
    }]
);
console.log(obj);

let result = obj.filter(item => item._id == 1)[0];
console.log(result);

结果是:

Object {0:Object,1:Object,2:Object,3:Object,4:Object,5:Object,6:Object,7:Object,8:Object} 未捕获(承诺)TypeError:obj.find不是函数(...)

注意:obj没有在函数中定义,它来自另一个系统,但我写的是显示你的缺陷

1 个答案:

答案 0 :(得分:1)

我无法在Array上使用Object的方法。假设您收到obj变量作为对象。您应该将其转换为数组。试试这个:

Object.keys(obj).map((i) => obj[i]).filter(e => e._id === 1)