Find value in array of objects

James

I have an array of objects, each object contains n key/value pairs. I need to return an array of the objects which has a value matching x.

Using Underscore.js I could use _.findWhere however I don't know what key the value will be under.

I could obviously loop the array, fetch all of the keys in each object, then run _.findWhere on each key and check if the value is there, but it doesn't seem like a good way of doing this.

Moritz Roessler

I could obviously loop the array, fetch all of the keys in each object...

Yes.

Write a function that accepts an array and a value to look for in its elements members, loop over the array, loop over the keys of the current element, and push the objects containing a member with a matching value to an array and return it after the iteration.

function findValues (arr,val) {
    var result = [];
    for (var i=0,current;i<arr.length;i++) {
        current = arr [i];
        for (var key in current) {
            if (current [key] === val) {
               result.push (current);
            }
        }
    }
    return result
}

Here is an example output

findValues (
   [{
     a:1,
     b:2,
     c:3
   },{
     a:1,
     b:2,
   },{
     a:1,
     b:2,
   },{
     a:1,
     b:2,
     c:3,
     d:4
   },{
     a:1,
     b:2,
   }],
   3
) //[{"a":1,"b":2,"c":3},{"a":1,"b":2,"c":3,"d":4}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Find a value in an array of objects in Javascript

From Dev

Find maximum value of an attribute in a array of objects

From Dev

Duplicate value in javascript array of objects is not recognized by find()

From Dev

Best way to find smallest value from array containing objects

From Dev

How to find largest value from array of NSDictionary objects?

From Dev

jquery filter array of objects to find object property with highest value

From Dev

How to find all objects with a false value within an array?

From Dev

Find index of javascript "array of objects" based on object field value

From Dev

Loop through array of objects to find if one value differs

From Dev

Trying to find object in an array of objects that has a field of a certain value

From Dev

Find specific key value in array of objects using nodejs

From Dev

Find max value in array of objects containing NaN values

From Java

How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

From Dev

Add value into an array of Objects

From Dev

Pushing a value to an array of objects

From Dev

Searching for a value in an Array of objects

From Dev

Find duplicate object based on value property in array of objects and then reduce the array concatenating the duplicate object in its label property

From Dev

Cycle through array of objects, find matching value and then create new array containing each set of matched values

From Dev

Find objects with field value (this is a pointer)

From Dev

How to compare array of objects to find missing objects

From Dev

How to find by array of objects in Mongoose?

From Dev

Find min, max in array with objects

From Dev

How to find by array of objects in Mongoose?

From Dev

Find duplicated element in array of objects

From Dev

Find min value in array

From Dev

Find the value that is in both array

From Dev

Find value in array of structs

From Dev

Find the position of a value in an array

From Dev

Find a value in array

Related Related

  1. 1

    Find a value in an array of objects in Javascript

  2. 2

    Find maximum value of an attribute in a array of objects

  3. 3

    Duplicate value in javascript array of objects is not recognized by find()

  4. 4

    Best way to find smallest value from array containing objects

  5. 5

    How to find largest value from array of NSDictionary objects?

  6. 6

    jquery filter array of objects to find object property with highest value

  7. 7

    How to find all objects with a false value within an array?

  8. 8

    Find index of javascript "array of objects" based on object field value

  9. 9

    Loop through array of objects to find if one value differs

  10. 10

    Trying to find object in an array of objects that has a field of a certain value

  11. 11

    Find specific key value in array of objects using nodejs

  12. 12

    Find max value in array of objects containing NaN values

  13. 13

    How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

  14. 14

    Add value into an array of Objects

  15. 15

    Pushing a value to an array of objects

  16. 16

    Searching for a value in an Array of objects

  17. 17

    Find duplicate object based on value property in array of objects and then reduce the array concatenating the duplicate object in its label property

  18. 18

    Cycle through array of objects, find matching value and then create new array containing each set of matched values

  19. 19

    Find objects with field value (this is a pointer)

  20. 20

    How to compare array of objects to find missing objects

  21. 21

    How to find by array of objects in Mongoose?

  22. 22

    Find min, max in array with objects

  23. 23

    How to find by array of objects in Mongoose?

  24. 24

    Find duplicated element in array of objects

  25. 25

    Find min value in array

  26. 26

    Find the value that is in both array

  27. 27

    Find value in array of structs

  28. 28

    Find the position of a value in an array

  29. 29

    Find a value in array

HotTag

Archive