javascript - underscore filter array of nested objects -
i have following array of objects:
[{key: "test", values: [{key: "2015-05", values: [{1:2}, {3:4}]}, {}], ...]
i want filter key
value left values after date.
i have this, resulting structure wrong
_.map(temp1, function(value, key) { return _.filter(value.values, function(v) { return v.key > "2015-05"; }); });
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 = _.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 parse date format, string comparison suffice. important note, despite _.map
operation, modify original.
Comments
Post a Comment