How to filter arrays depending on the values of arrays inside the array?

scaryguy

Why doesn't this test pass?

it('lodash_test', function() {
    var arr = [{a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "33", y: "2"}]}]
    var result = _.filter(arr, function(obj) {
        obj.b.forEach(function(item_b) {
            if(item_b.x=="1") {
                return true;
            }
        });
    });
    expect(result).to.be.eql([{a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "1", y: "2"}]}]);
});
T.J. Crowder

Because forEach's return value is completely ignored, and your _.filter predicate function never returns anything.

You probably wanted to use some:

it('lodash_test', function() {
    var arr = [{a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "33", y: "2"}]}]
    var result = _.filter(arr, function(obj) {
        return obj.b.some(function(item_b) {
    //  ^^^^^^^      ^^^^
            if(item_b.x=="1") {
                return true;
            }
        });
    });
    expect(result).to.be.eql([{a: "x", b: [{x: "1", y: "2"}]}, {a: "x", b: [{x: "1", y: "2"}]}]);
});

Array#some calls the predicate for each entry in the array, stopping the first time the predicate returns a truthy value; its return value is true if the predicate ever returned a truthy value, or false if not.

So the return I've underscored in the above is the return from the _.filter predicate; your original return true is the return from some (formerly forEach).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to write filter that inspects an array of arrays

分類Dev

How to count items in arrays of values in an array of hashes

分類Dev

How to Find the Differences Values Inside Arrays and Log the Index of the Differences

分類Dev

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

分類Dev

Inserting values randomly into an array of arrays

分類Dev

How to sum values column wise for Array of Arrays in Scala?

分類Dev

PHP - How to remove duplicate values from array (compare 2 arrays)

分類Dev

How to iterate through values in an array containing arrays in PHP

分類Dev

remove duplicate elements in proceeding arrays inside array of arrays

分類Dev

How to compare an array to an array of arrays?

分類Dev

How to insert an array into an array of arrays?

分類Dev

Filter Array of Arrays in JavaScript to become unique

分類Dev

Update field of a document inside an array of arrays in mongodb

分類Dev

How to convert an array of arrays into a matrix?

分類Dev

Javascript: Map values of two arrays in a new array

分類Dev

Create an Array with a KEY if values exists in two arrays?

分類Dev

How do I return 2 values from two arrays when using filter

分類Dev

nodejs how I filter an arrays of objects

分類Dev

compare values in arrays and if any values match, increment values in first array

分類Dev

How to insert a numpy array to a numpy array of arrays?

分類Dev

How to get common values from 4 multidimensional arrays using array_intersect

分類Dev

How to get an array with true/false values after comparing 2 arrays in ruby?

分類Dev

Filtering two arrays with .filter

分類Dev

Create sub-arrays depending on object values (Two groupings for two objects)

分類Dev

Dealing with hashes inside arrays

分類Dev

How to check bordering duplicates in an array of arrays

分類Dev

How to count occurrences of strings in array of arrays?

分類Dev

How to array_unique dimensional arrays

分類Dev

How to send array of arrays as data in $.ajax()?

Related 関連記事

  1. 1

    How to write filter that inspects an array of arrays

  2. 2

    How to count items in arrays of values in an array of hashes

  3. 3

    How to Find the Differences Values Inside Arrays and Log the Index of the Differences

  4. 4

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

  5. 5

    Inserting values randomly into an array of arrays

  6. 6

    How to sum values column wise for Array of Arrays in Scala?

  7. 7

    PHP - How to remove duplicate values from array (compare 2 arrays)

  8. 8

    How to iterate through values in an array containing arrays in PHP

  9. 9

    remove duplicate elements in proceeding arrays inside array of arrays

  10. 10

    How to compare an array to an array of arrays?

  11. 11

    How to insert an array into an array of arrays?

  12. 12

    Filter Array of Arrays in JavaScript to become unique

  13. 13

    Update field of a document inside an array of arrays in mongodb

  14. 14

    How to convert an array of arrays into a matrix?

  15. 15

    Javascript: Map values of two arrays in a new array

  16. 16

    Create an Array with a KEY if values exists in two arrays?

  17. 17

    How do I return 2 values from two arrays when using filter

  18. 18

    nodejs how I filter an arrays of objects

  19. 19

    compare values in arrays and if any values match, increment values in first array

  20. 20

    How to insert a numpy array to a numpy array of arrays?

  21. 21

    How to get common values from 4 multidimensional arrays using array_intersect

  22. 22

    How to get an array with true/false values after comparing 2 arrays in ruby?

  23. 23

    Filtering two arrays with .filter

  24. 24

    Create sub-arrays depending on object values (Two groupings for two objects)

  25. 25

    Dealing with hashes inside arrays

  26. 26

    How to check bordering duplicates in an array of arrays

  27. 27

    How to count occurrences of strings in array of arrays?

  28. 28

    How to array_unique dimensional arrays

  29. 29

    How to send array of arrays as data in $.ajax()?

ホットタグ

アーカイブ