JSFiddle - React, Tailwind, and code Playground

by John Grivas

JavaScript

var data = [{
    "AllQuotes": [{
        "Quote": "Life is Bad",
            "Attributes": {
            "StatedYear": 1999,
                "smart": true,
                "inspiring": false
        }
    }, {
        "Quote": "Life is Good",
            "Attributes": {
            "StatedYear": 1972,
                "smart": false,
                "inspiring": true
        }
    }, {
        "Quote": "Let's Party",
            "Attributes": {
            "StatedYear": 1999,
                "smart": false,
                "inspiring": true
        }
    }, {
        "Quote": "All is a Game",
            "Attributes": {
            "StatedYear": 1952,
                "smart": true,
                "inspiring": false
        }
    }]
}];
var getSpecificObjects = function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(obj);
        } else if (obj[i] == val && key == '') {
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1) {
                objects.push(obj);
            }
        }
    }
    return objects;
}
//First one  pull all quotes from 1999
var allQuotes1999 = getSpecificObjects(data, "StatedYear", "1999");
console.log(allQuotes1999);
//all quotes with attribute inspiring = true
var allQuotesInspiring  = getSpecificObjects(data, "inspiring",true);
console.log(allQuotesInspiring);