filter array of objects with underscore and get new array of values

lunacafu

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.

Jon

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Sum values of objects which are inside an array with underscore.js and reduce

分類Dev

Angular - filter an array of objects based on values in another array of objects

分類Dev

Underscore JS difference on array and array with objects

分類Dev

How to get all values of objects inside array

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Re-structuring an array of objects with Underscore

分類Dev

Filter array of objects using a list of desired key values

分類Dev

Creating a new array of objects by finding values in JSON data

分類Dev

Mapping array of objects into new array of array by value

分類Dev

es6 filter an array of objects by an array of filter objects

分類Dev

How to multi filter an array of objects?

分類Dev

filter two array of objects in javascript

分類Dev

Swift 3 filter array of objects with elements of array

分類Dev

Filter specific elements of ordered array of objects, based on values of previous objects (aggregation framework)

分類Dev

Compare an Array to an Array of Objects and Return a new Array of Objects with Grouped Dates

分類Dev

'push' new number into an array of objects

分類Dev

javascript : filter array of objects using another array as filter

分類Dev

How can I get a new array filtered by value in another array of objects? javascript

分類Dev

Add new array values to existing array position

分類Dev

replace key with new array values

分類Dev

Counting occurrence of values in an array of objects in another array of objects

分類Dev

Filter an array of objects by a property containing a substring in AngularJS

分類Dev

Filter javascript objects array based on a multivalue object

分類Dev

Filter array of objects - erase last word in the string

分類Dev

How do i filter an array inside of a array of objects?

分類Dev

Sort array of objects based on new data

分類Dev

How to get unique an array of objects back from a complex an array of objects?

分類Dev

How to get values in array of array in angular?

分類Dev

How to get text values to an array?

Related 関連記事

  1. 1

    Sum values of objects which are inside an array with underscore.js and reduce

  2. 2

    Angular - filter an array of objects based on values in another array of objects

  3. 3

    Underscore JS difference on array and array with objects

  4. 4

    How to get all values of objects inside array

  5. 5

    How to get the combination of array values from nested arrays in an array of objects

  6. 6

    Re-structuring an array of objects with Underscore

  7. 7

    Filter array of objects using a list of desired key values

  8. 8

    Creating a new array of objects by finding values in JSON data

  9. 9

    Mapping array of objects into new array of array by value

  10. 10

    es6 filter an array of objects by an array of filter objects

  11. 11

    How to multi filter an array of objects?

  12. 12

    filter two array of objects in javascript

  13. 13

    Swift 3 filter array of objects with elements of array

  14. 14

    Filter specific elements of ordered array of objects, based on values of previous objects (aggregation framework)

  15. 15

    Compare an Array to an Array of Objects and Return a new Array of Objects with Grouped Dates

  16. 16

    'push' new number into an array of objects

  17. 17

    javascript : filter array of objects using another array as filter

  18. 18

    How can I get a new array filtered by value in another array of objects? javascript

  19. 19

    Add new array values to existing array position

  20. 20

    replace key with new array values

  21. 21

    Counting occurrence of values in an array of objects in another array of objects

  22. 22

    Filter an array of objects by a property containing a substring in AngularJS

  23. 23

    Filter javascript objects array based on a multivalue object

  24. 24

    Filter array of objects - erase last word in the string

  25. 25

    How do i filter an array inside of a array of objects?

  26. 26

    Sort array of objects based on new data

  27. 27

    How to get unique an array of objects back from a complex an array of objects?

  28. 28

    How to get values in array of array in angular?

  29. 29

    How to get text values to an array?

ホットタグ

アーカイブ