-4

I have a nested JSON file like this...

{
    "A": {
        "Status": "Hard",
        "Level": "L1",
        "Capacity": "NR"
    },
    "B": {
        "Status": "Medium",
        "Level": "L2",
        "Capacity": "BR"
    },
    "C": {
        "Status": "Low",
        "Level": "L3",
        "Capacity": "SR"
    },
    "D": {
        "Status": "",
        "Level": "",
        "Capacity": "ZR"
    }
} 

What I am looking to do is retreive the names of the key, when the Status and Level property are present or not empty. The length of the nested object can be variable.

Desired output:

A, B, C

I have looked at this solution, but I am not sure how to test for the properties ? Many thanks in advance.

Slyper
  • 896
  • 2
  • 15
  • 32

2 Answers2

2

const obj = {
    A: {
        Status: 'Hard',
        Level: 'L1',
        Capacity: 'NR',
    },
    B: {
        Status: 'Medium',
        Level: 'L2',
        Capacity: 'BR',
    },
    C: {
        Status: 'Low',
        Level: 'L3',
        Capacity: 'SR',
    },
    D: {
        Status: '',
        Level: '',
        Capacity: 'ZR',
    },
};

const result = Object.keys(obj).filter(key => obj[key].Level && obj[key].Status);

console.log(result);
Nikita Madeev
  • 4,284
  • 9
  • 20
0

const obj = {
    A: {
        Status: 'Hard',
        Level: 'L1',
        Capacity: 'NR',
    },
    B: {
        Status: 'Medium',
        Level: 'L2',
        Capacity: 'BR',
    },
    C: {
        Status: 'Low',
        Level: 'L3',
        Capacity: 'SR',
    },
    D: {
        Status: '',
        Level: '',
        Capacity: 'ZR',
    },
};

const result = [];

for (k in obj) {
   if (obj[k].Status && obj[k].Level) {
      result.push(k)
   }
}

console.log(result);
codedawi
  • 314
  • 2
  • 11