Skip to content
Advertisement

Searching Objects for Values and placing into new array

I’m attempting to search each of the objects listed below from my dataset to see if they contain a value and if they do have that value to add the entire object into a new array.

Example:

Search List of Objects for Data “Continuous”, if that data exists (Which it does in Doormerica and Hager) then take the entire object “Doormerica” and “Hager” and put the entire thing into a new array.

{
    "Doormerica": {
        "Floor Stops": [],
        "Overhead Stop": [],
        "Pull": [],
        "Chain Stop": [],
        "Continuous": [
            "ALX",
            "AL",
            "QR"
        ],
        "Kick": [],
        "Back to Back Pull": [],
        "Concealed": [],
        "Butt": [],
        "Surface Mount": [],
        "Mop": [],
        "Armor": [],
        "Push": [],
        "Wall Stops": []
    },
    "Schlage": {
        "Mortise": [],
        "Cylindrical": [
            "ALX",
            "AL",
            "QR"
        ],
        "Deadbolt": [],
        "Dummy": [],
        "Interconnected": [],
        "Cylinders": []
    },
    "Pemko": {
        "Sweeps": [
            "345AV"
        ],
        "Permiter Seal": [
            "303AS"
        ],
        "Thresholds": [
            "170A"
        ],
        "Rain Drip": [
            "346C"
        ],
        "Astragal": []
    },
    "LCN": {
        "Surface Mount": [
            "4040XP"
        ],
        "Concealed": []
    },
    "Hager": {
        "Butt": [],
        "Continuous": []
    }
}

Advertisement

Answer

Data Transformation

I decided to post this solution because the question asks how to “take the entire object”. And including the name (Doormerica, Hager, etc.) in the result, either as a key or a value, seems an important part of the transformation. And we can accomplish this in one pass using Object.entries(), Array.reduce(), Spread Syntax, and the appropriate transformation code.

Examples of both transformations

[{"Doormerica": { "Floor Stops": [], ...}]  // name as a key

[{ "Name": "Doormerica", "Floor Stops": [], ...}] // name as a value

Snippet

The snippet shows the complete code for doing both transformations.

const data = {Doormerica:{"Floor Stops":[],"Overhead Stop":[],Pull:[],"Chain Stop":[],Continuous:["ALX","AL","QR"],Kick:[],"Back to Back Pull":[],Concealed:[],Butt:[],"Surface Mount":[],Mop:[],Armor:[],Push:[],"Wall Stops":[]},Schlage:{Mortise:[],Cylindrical:["ALX","AL","QR"],Deadbolt:[],Dummy:[],Interconnected:[],Cylinders:[]},Pemko:{Sweeps:["345AV"],"Permiter Seal":["303AS"],Thresholds:["170A"],"Rain Drip":["346C"],Astragal:[]},LCN:{"Surface Mount":["4040XP"],Concealed:[]},Hager:{Butt:[],Continuous:[]}};


let subset = Object.entries(data).reduce((a,v) =>
  
  v[1].hasOwnProperty("Continuous") ? [...a, {[v[0]]: v[1]}  ] : a
  
  // alternative methods adds a name property
  // v[1].hasOwnProperty("Continuous") ? [...a, {Name: v[0], ...v[1]}  ] : a
  
, []);

console.log(subset);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement