简体   繁体   English

JavaScript:从对象数组中过滤掉键

[英]JavaScript: Filter out keys from array of objects

This is what my current object looks like.这就是我现在的 object 的样子。 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.我的问题是如何解析这个 object 以便它返回预期的 output (没有索引 0,1,2 ...)我想要一个 Javascript 对象数组,没有键

 "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.没有索引 0,1,2 并且应该在一个数组中。 So I want the output as array of objects所以我想要 output 作为对象数组

   "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.您可以使用 Object.values 来获取您想要的数组。

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

Just use Object.values for contacts property只需将Object.values用于联系人属性

 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. Object.values()方法返回给定对象自己的可枚举属性值的数组。 In this case, the object to be passed is contacts from the original data.在这种情况下,要传递的 object 是来自原始数据的contacts

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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