I do have an array of objects and I want to create a filter function to return an new array
var items = [{
"row": 0,
"column": 0,
"items": [2, 3, 4, 5, 6, 7]
}, {
"row": 0,
"column": 1,
"items": [8, 9, 10, 11, 12, 13]
....
{
"row": 2,
"column": 2,
"items": [50, 51, 52, 53, 54, 55]
}]
var newArray = function (items, row) {
//filter items and return new array
return filtered
}
newArray should contain all values from 'items' that have the same row value.
I do have an array of objects and I want to create a filter function to return an new array
var items = [{
"row": 0,
"column": 0,
"items": [2, 3, 4, 5, 6, 7]
}, {
"row": 0,
"column": 1,
"items": [8, 9, 10, 11, 12, 13]
....
{
"row": 2,
"column": 2,
"items": [50, 51, 52, 53, 54, 55]
}]
var newArray = function (items, row) {
//filter items and return new array
return filtered
}
newArray should contain all values from 'items' that have the same row value.
Share Improve this question edited Dec 5, 2013 at 13:48 Dhaval Marthak 17.4k6 gold badges48 silver badges69 bronze badges asked Dec 5, 2013 at 13:45 lunacafulunacafu 3668 silver badges21 bronze badges 7- Hey, wele to StackOverflow. It would help if you explained what you have tried so far and why it didn't work for you. – Tibos Commented Dec 5, 2013 at 13:46
- Tried to group them with var rowGrps = _.groupBy(items, function(p){ return p.row; }); but didn't feel right – lunacafu Commented Dec 5, 2013 at 13:48
- Could you demonstrate the result you're expecting to be created from this? – Jonathan Lonowski Commented Dec 5, 2013 at 13:50
-
1
@lunacafu: But that's just what you want? How else should the expected output look like? Btw, you could shorten that to
_.groupBy(items, "row")
– Bergi Commented Dec 5, 2013 at 13:50 -
Question is confusing because the top-level array is called
items
, but also each item contains a property nameditems
– broofa Commented Dec 5, 2013 at 13:55
3 Answers
Reset to default 3If I understand the question correctly, the result would be given by
function filter(items, row) {
return _.chain(items) // initiate method chaining for convenience
.where({row: row}) // filter out objects from rows we don't care about
.pluck("items") // get the "items" arrays from the filtered objects
.flatten() // concatenate them into a single array
.value(); // unwrap the result to return it
}
Calling filter(items, 0)
in the example given would return
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
which is the concatenated aggregate of items
arrays inside objects with row
equal to 0.
Maybe you're looking for this:
_.reduce(items, function(res, item) {
var key = item.row;
if (!(key in res)) res[key] = [];
[].push.apply(res[key], item.items);
return res;
}, {})
In underscore, to get all items where the row value matches the index of the item in the array:
var items = [...];
var filteredItems = _.filter(items, function(item, i) {
return item.row == i;
});
Or with the native Array.prototype.map method. E.g.
var items = [...];
var filteredItems = items.filter(function(item, i) {
return item.row == i;
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745253168a4618798.html
评论列表(0条)