user6784840
user6784840

Reputation:

Write a function that takes an array of objects and an object with properties & values

Write a function that takes an array of objects and an object with properties and values. The function will return an array of objects where the prop/vals match.

I know the following, but am having a lot of trouble understanding the question itself. Any help is appreciated!

An example return would be :

thinglist = [
{age: 'four', type: 'Cat', name: 'mammal'},
{age: 'six', type: 'Dog', name: 'mammal'},
{age: 'seven', type: 'Dog', name: 'mammal'},
{age: 'two', type: 'Lizard', name: 'reptile'}
]


withVals( thingList , {type: 'Dog', name: 'mammal'} )
=> [
{age: 'seven', type: 'Dog', name: 'mammal'},
{age: 'six', type: 'Dog', name: 'mammal'}
]

I know how to iterate over the array and pull out the values, but I have no idea how to pass in an object to the function as well. Then iterate over it to see if those values match.

This is how I see it starting:

function withPropValues(arr, obj) {
    var newArray = [];
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === obj) {
            newArray.push(arr[i])
        }
    }
    return newArray;
}

Upvotes: 0

Views: 453

Answers (2)

遁地龙卷风
遁地龙卷风

Reputation: 40

var thingList = [
    {age: 'four', type: 'Cat', name: 'mammal'},
    {age: 'six', type: 'Dog', name: 'mammal'},
    {age: 'seven', type: 'Dog', name: 'mammal'},
    {age: 'two', type: 'Lizard', name: 'reptile'}
    ];
    withPropValues(thingList, {type: 'Cat', name: 'mammal'})
    function withPropValues(arr, obj) 
    {
    var newArray = [],
        sum = 0;
    for(var c in obj)
    {
        sum++;
    }
    for (var i = 0; i < arr.length; i++) 
    {
        var match=0;
        for(var prop in arr[i])
        {
            for(c in obj)
            {
                if(obj[c] == arr[i][prop])
                {
                    match++;
                    break;
                }
            }
            if(match == sum) 
            {
                newArray.push(arr[i]);
                break;
            }
        }
    }
    return newArray;
}

Upvotes: 0

apsillers
apsillers

Reputation: 115940

You need to loop over all the properties of object and see if they match the same-named properties of each item in array. You can get all the property names with Object.keys(object), and from there it's a matter of accessing the values with bracket notation, e.g., object[key] === array[i][key].

Generally, what you're trying to do could be accomplished very neatly with filter, and you could match all properties with an every over Object.keys:

function withVals(array, object) {
    var keys = Object.keys(object);
    return array.filter(function(item) {
        return keys.every(function(k) { return item[k] === object[k]; });
    });
}
  • filter takes a callback which examines an item from the array and returns true if the item should be kept or false if it should not be kept. It returns a new filtered array.

  • every takes a callback that examines an item of an array and returns either true or false. If each item in the array causes the function to return true, then the every call also returns true, but a single false value causes it to return false.

  • The Object.keys(object).every is testing if every key of object has a value that matches the corresponding property value in some item of array. If so, include that item in the filtered version of the array.

Upvotes: 2

Related Questions