React JSX Question

filter an object JS

I'm trying to filter out an object, but does not work ....

This is the code:

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);


the result is:

Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object, 8: Object}
Uncaught (in promise) TypeError: obj.find is not a function(…)

note: obj is not defined in the function, it comes from another system, but I write to show your defición

Answer

I can't use methods of Array on Object. Assume you receipt obj variable as an Object. You should convert it to an array. Try this:

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