简体   繁体   中英

JavaScript: Filter out keys from array of objects

This is what my current object looks like. My question is how to parse this object such that it will return the expected output (without index 0,1,2...) I want a Javascript array of objects, without the key value.

 "contacts": {
  "0": {
    "firstName": "James",
    "lastName": "April",
    "emails": [
      {
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": ""
      }
    ],
    "relationship": "boyfriend",
    "phones": [
      {
        "phoneType": "HOME",
        "phoneNumber": "(456) 888-9999"
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(789) 123-4567"
      },
    ],
    "callSequence": 0,
    "note": null
  },
  "1": {
    "firstName": "Joe",
    "lastName": "Kimmel",
    "emails": [
      {
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": ""
      }
    ],
    "relationship": "ex-husband",
    "phones": [
      {
        "phoneType": "HOME",
        "phoneNumber": ""
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(111) 111-1111"
      },
      {
        "phoneType": "WORK",
        "phoneNumber": ""
      }
    ],
    "callSequence": 0,
    "note": null
  },
  "2": {
    "firstName": "Test",
    "lastName": "",
    "emails": [
      {
        "emailType": "WORK",
        "address": "test@gmail.com"
      },
      {
        "emailType": "PERSONAL",
        "address": "test2@gmail.com"
      }
    ],
    "relationship": "BROTHER-IN-LAW",
    "phones": [
      {
        "phoneType": "HOME",
        "phoneNumber": ""
      },
      {
        "phoneType": "CELL",
        "phoneNumber": ""
      },
      {
        "phoneType": "WORK",
        "phoneNumber": ""
      }
    ],
    "callSequence": 0
  }
}

}

Expected Result
Wihout the indexes 0,1,2 and should be inside an array. So I want the output as array of objects

   "contacts": [{
   {
    "firstName": "James",
    "lastName": "April",
    "emails": [
      {
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": ""
      }
    ],
    "relationship": "boyfriend",
    "phones": [
      {
        "phoneType": "HOME",
        "phoneNumber": "(456) 888-9999"
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(789) 123-4567"
      },
    ],
    "callSequence": 0,
    "note": null
  },
   {
    "firstName": "Joe",
    "lastName": "Kimmel",
    "emails": [
      {
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": ""
      }
    ],
    "relationship": "ex-husband",
    "phones": [
      {
        "phoneType": "HOME",
        "phoneNumber": ""
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(111) 111-1111"
      },
      {
        "phoneType": "WORK",
        "phoneNumber": ""
      }
    ],
    "callSequence": 0,
    "note": null
  }]

You can use Object.values to get the array you're after.

const newObject = {contacts: Object.values(oldObject.contacts)};

Just use Object.values for contacts property

 let data = {"contacts": { "0": { "firstName": "James", "lastName": "April", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "boyfriend", "phones": [ { "phoneType": "HOME", "phoneNumber": "(456) 888-9999" }, { "phoneType": "CELL", "phoneNumber": "(789) 123-4567" }, ], "callSequence": 0, "note": null }, "1": { "firstName": "Joe", "lastName": "Kimmel", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "ex-husband", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "(111) 111-1111" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0, "note": null }, "2": { "firstName": "Test", "lastName": "", "emails": [ { "emailType": "WORK", "address": "test@gmail.com" }, { "emailType": "PERSONAL", "address": "test2@gmail.com" } ], "relationship": "BROTHER-IN-LAW", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0 } } } data.contacts = Object.values(data.contacts); console.log(data)

The Object.values() method returns an array of a given object's own enumerable property values. In this case, the object to be passed is contacts from the original data.

 var data = { "contacts": { "0": { "firstName": "James", "lastName": "April", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "boyfriend", "phones": [ { "phoneType": "HOME", "phoneNumber": "(456) 888-9999" }, { "phoneType": "CELL", "phoneNumber": "(789) 123-4567" }, ], "callSequence": 0, "note": null }, "1": { "firstName": "Joe", "lastName": "Kimmel", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "ex-husband", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "(111) 111-1111" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0, "note": null }, "2": { "firstName": "Test", "lastName": "", "emails": [ { "emailType": "WORK", "address": "test@gmail.com" }, { "emailType": "PERSONAL", "address": "test2@gmail.com" } ], "relationship": "BROTHER-IN-LAW", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0 } } }; data = { contacts: Object.values(data.contacts) } console.log(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM