我确实有一个对象数组,我想创建一个过滤函数来返回一个新数组
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 应包含具有相同行值的“items”中的所有值。
最佳答案
如果我正确理解了问题,结果将是
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
}
在给定的示例中调用 filter(items, 0)
将返回
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
这是对象内 items
数组的串联聚合,其中 row
等于 0。
关于javascript - 过滤带下划线的对象数组并获取新的值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401627/