拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 JavaScript:从物件阵列中过滤掉键

JavaScript:从物件阵列中过滤掉键

白鹭 - 2022-01-26 1972 0 0

这就是我当前物件的样子。 我的问题是如何决议这个物件,使其回传预期的输出(没有索引 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
  }
}

}

预期结果
没有索引 0,1,2 并且应该在阵列内。所以我希望输出为物件阵列

   "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
  }]

uj5u.com热心网友回复:

您可以使用 Object.values 来获取您想要的阵列。

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

uj5u.com热心网友回复:

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)

uj5u.com热心网友回复:

Object.values ()方法回传给定物件自己的可列举属性值的阵列。在这种情况下,要传递的物件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)

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *