我有一个这样的对象,我想获得例如所有公司的简单列表。之后,将代码过滤公司在一个特定的城市或企业开始"N" ?
[
{
"fieldData":
{
"City": "Nashville",
"Company": "Bohnen, Thomas P Esq",
"FirstName": "Cara",
"LastName": "Hingst",
"id": "B3840265-9363-4730-A6F4-D7F8DEACD5C3"
},
"modId": "5",
"portalData": {},
"recordId": "202"
},
{
"fieldData":
{
"City": "San Francisco",
"Company": "Monroeville Area Chmbr Commrce",
"FirstName": "Diane",
"LastName": "Ort",
"id": "DEB98ADD-1F65-473F-AC14-62D1FCBC0A47"
},
"modId": "2",
"portalData": {},
"recordId": "203"
},
{
"fieldData":
{
"City": "Charlottesville",
"Company": "Parks Arizona State",
"FirstName": "Zane",
"LastName": "Opunui",
"id": "74B41283-B468-49FC-9AC0-69A675E56821"
},
"modId": "2",
"portalData": {},
"recordId": "204"
},
{
"fieldData":
{
"City": "Las Vegas",
"Company": "Shick And Wesley",
"FirstName": "Lea",
"LastName": "Knighton",
"id": "4C37361D-D418-4973-9B2B-A6ACCF31F1C5"
},
"modId": "2",
"portalData": {},
"recordId": "205"
},
{
"fieldData":
{
"City": "Franklin",
"Company": "Micro Enterprises",
"FirstName": "Patsy",
"LastName": "Rezac",
"id": "7E50FD85-6EB5-4857-B671-E178C189052E"
},
"modId": "2",
"portalData": {},
"recordId": "206"
}
]
要获得所有公司名称的列表,您应该使用.map()方法。如果您想获得特定的公司名称,您应该首先使用.filter()过滤数据方法,然后以相同的方式映射这些数据。
const object = [{ "fieldData": { "City": "Nashville", "Company": "Bohnen, Thomas P Esq", "FirstName": "Cara", "LastName": "Hingst", "id": "B3840265-9363-4730-A6F4-D7F8DEACD5C3" }, "modId": "5", "portalData": {}, "recordId": "202" }, { "fieldData": { "City": "San Francisco", "Company": "Monroeville Area Chmbr Commrce", "FirstName": "Diane", "LastName": "Ort", "id": "DEB98ADD-1F65-473F-AC14-62D1FCBC0A47" }, "modId": "2", "portalData": {}, "recordId": "203" }, { "fieldData": { "City": "Charlottesville", "Company": "Parks Arizona State", "FirstName": "Zane", "LastName": "Opunui", "id": "74B41283-B468-49FC-9AC0-69A675E56821" }, "modId": "2", "portalData": {}, "recordId": "204" }, { "fieldData": { "City": "Las Vegas", "Company": "Shick And Wesley", "FirstName": "Lea", "LastName": "Knighton", "id": "4C37361D-D418-4973-9B2B-A6ACCF31F1C5" }, "modId": "2", "portalData": {}, "recordId": "205" }, { "fieldData": { "City": "Franklin", "Company": "Micro Enterprises", "FirstName": "Patsy", "LastName": "Rezac", "id": "7E50FD85-6EB5-4857-B671-E178C189052E" }, "modId": "2", "portalData": {}, "recordId": "206" }]
const companies = object.map((e) => e.fieldData.Company)
console.log(companies)
const filteredCompanies = object.filter((e) => e.fieldData.City === 'Las Vegas');
const companies_from_specific_city = filteredCompanies.map((e) => e.fieldData.Company)
console.log(companies_from_specific_city)