-3

I have an object like this and I want to get for exemple a simple list of all companies. After, what would be the code to filter the companies in a specific city or the companies starting with "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"
  }
]
pilchard
  • 12,414
  • 5
  • 11
  • 23
  • 1
    and [Filter a list element starting with a letter in JavaScript](https://stackoverflow.com/questions/40454296/filter-a-list-element-starting-with-a-letter-in-javascript) – pilchard Jul 15 '22 at 09:13
  • also [Get JavaScript object from array of objects by value of property](https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property) – pilchard Jul 15 '22 at 09:20
  • https://stackoverflow.com/help/how-to-ask <= You're question doesn't meet the guideline of stackoverflow, you need to provide what you tried so we can help you. It'll be great if you follow this for your next question – Nicolas Menettrier Jul 15 '22 at 09:32

1 Answers1

1

To get a list of all companies' names you should use the .map() method. If you want to get specific companies' names you should first filter your data with the .filter() method and then map those data the same way as before.

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)
pilchard
  • 12,414
  • 5
  • 11
  • 23
TymoteuszLao
  • 844
  • 1
  • 5
  • 16