Home > database >  how to map a json object into another new object
how to map a json object into another new object

Time:11-30

i have a payload response which goes like:

"requirementDetails": {
            "requirementId1": "A",
            "requirementId2": "B",
            "requirementId3": "C",
            "requirementId4": "D",
            "requirementId5": "E",
            "requirementId6": "",
            "requirementId7": "",
            "requirementId8": "",
            "requirementId9": "",
        }

and i want to create new array of the requirements which will not take the blank requirements using javascript map function and i want the resultant array to look like this:

data = [ "A","B","C","D","E"]

can anybody please help me out.

CodePudding user response:

You can get all values using Object.values and then you can filter it

const payload = {
  requirementDetails: {
    requirementId1: "A",
    requirementId2: "B",
    requirementId3: "C",
    requirementId4: "D",
    requirementId5: "E",
    requirementId6: "",
    requirementId7: "",
    requirementId8: "",
    requirementId9: "",
  },
};

const data = Object.values(payload.requirementDetails).filter(Boolean);

console.log(data);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related