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.
If 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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加