Reputation: 2361
I have the following array of objects:
[{key: "test", values: [{key: "2015-05", values: [{1:2}, {3:4}]}, {}], ...]
I want to filter by the key
value to only be left with values after a certain date.
I currently have this, but the resulting the structure is wrong
_.map(temp1, function(value, key) { return _.filter(value.values, function(v) { return v.key > "2015-05"; }); });
Upvotes: 0
Views: 1252
Reputation: 2856
Slight improvement on Frank's answer:
var data = [
{
key: "1",
values: [
{key: "2014-05", values: [{1:2}, {3:4}]},
{key: "2015-05", values: [{1:2}, {3:4}]},
{key: "2013-05", values: [{1:2}, {3:4}]}
]
},
{
key: "2",
values: [
{key: "2014-05", values: [{1:2}, {3:4}]},
{key: "2015-05", values: [{1:2}, {3:4}]},
{key: "2013-05", values: [{1:2}, {3:4}]}
]
}
];
var a = _.map(data, (d) => {
d.values = _.filter(d.values, (v) => v.key > "2014");
return d;
});
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
No need to parse this date format, a string comparison will suffice. Important to note, despite the _.map
operation, this will modify the original.
Upvotes: 1
Reputation: 348
Something like this maybe?
var data = [
{
key: "1",
values: [
{key: "2014-05", values: [{1:2}, {3:4}]},
{key: "2015-05", values: [{1:2}, {3:4}]},
{key: "2013-05", values: [{1:2}, {3:4}]}
]
},
{
key: "2",
values: [
{key: "2014-05", values: [{1:2}, {3:4}]},
{key: "2015-05", values: [{1:2}, {3:4}]},
{key: "2013-05", values: [{1:2}, {3:4}]}
]
}
];
var a = _.map(data, (d) => {
d.values = _.filter(d.values, (v) => {
return Date.parse(v.key) > Date.parse("2014");
});
return d;
});
Upvotes: 0