Immortal Star
Immortal Star

Reputation: 49

Iterating through array object Javascript

I am trying to iterate through array object. I want to return the whole object only with records that matches the type with the string I enter if it matches the 'type' key in object when key matches the key I pass in the function which in this case is billing. I am writing a javascript function in react which takes an array and string as parameter. I am not able to match the keys correctly.

If str is 'Billing', it should return all the records with type Billing.

  getAddress(arrayObj, str) {
      for(let add in arrayObj) {
                    if(arrayObj[add].type === str) {
              return arrayObj[add];
          }
          return null;
      }
  }



let billAddress = this.getAddress(this.address, 'Billing');

This is my Array Object:

this.address=[
          {
              "pk" : "223qknzngcpcbxxx8k4iibsff3547z8t",
              "Customerpk" : "eqmemppbykdey6gwvph99f9d22nmfrav",
              "type" : "Billing",
              "first_name" : "Dhrumit",
              "last_name" : "Sheth",
              "BusinessName" : null,
              "ResidenceName" : null,
              "urbanization" : "",
              "street_name" : "",
              "sedry_number" : "",
              "city" : "",
              "zip" : "",
              "zip4" : "",
              "countryID" : 2,
              "stateID" : "",
              "country" : "CA",
              "ContactPhoneNumber" : "",
              "created" : "2011-12-20 06:13:27",
              "street_no" : "",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : "NULL",
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2016-12-16 20:35:56",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "225dk9u4s83sxvjrywtehwnyupymqu7s",
              "Customerpk" : "4fawz76u68xd9vw8qu7dgk7fk7mhw2nw",
              "type" : "Billing",
              "first_name" : "Billy",
              "last_name" : "Howard",
              "BusinessName" : "OOMACA",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "LEDUC",
              "sedry_number" : "",
              "city" : "BROSSARD",
              "zip" : "J4Y 0K7",
              "zip4" : "",
              "countryID" : 2,
              "stateID" : "QC",
              "country" : "CA",
              "ContactPhoneNumber" : "",
              "created" : "2017-04-14 23:14:55",
              "street_no" : "100",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : "BLVD",
              "sedry_unit" : "",
              "LocationID" : "NULL",
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-04-14 23:14:55",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "224f5bgmejdygvkwphpyky3d9rasq5my",
              "Customerpk" : "j2tfp56uc7sswvzcwcyqpdfqsd8msrby",
              "type" : "Billing",
              "first_name" : "User",
              "last_name" : "Synthetic",
              "BusinessName" : "OOMA",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "Calle 93 #19-55",
              "sedry_number" : "",
              "city" : "Bogotá",
              "zip" : "110221",
              "zip4" : "",
              "countryID" : 6,
              "stateID" : "",
              "country" : "CO",
              "ContactPhoneNumber" : "",
              "created" : "2018-01-15 00:48:49",
              "street_no" : "",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2018-01-15 00:48:49",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "170bf1013c6c65a1897f7e9ee774d4d1",
              "Customerpk" : "zde4z4hkgshcwh59ub4qzh6wqf4bckyy",
              "type" : "Billing",
              "first_name" : "RSH",
              "last_name" : "PARTNER",
              "BusinessName" : null,
              "ResidenceName" : null,
              "urbanization" : "",
              "street_name" : "RADIOSHACK",
              "sedry_number" : "",
              "city" : "FORT WORTH",
              "zip" : "76102",
              "zip4" : "1964",
              "countryID" : 1,
              "stateID" : "TX",
              "country" : "US",
              "ContactPhoneNumber" : "6505666641",
              "created" : "1900-01-01 00:00:00",
              "street_no" : "300",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : "CIR",
              "sedry_unit" : "",
              "LocationID" : "NULL",
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2009-10-02 00:16:37",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "222389ui49sx7mp5vx7iexhqrefdkr84",
              "Customerpk" : "9uwi4zb7bivz6fd7vj42pyjevxzu6qrt",
              "type" : "Service",
              "first_name" : "JERMAINE",
              "last_name" : "COPELAND",
              "BusinessName" : null,
              "ResidenceName" : null,
              "urbanization" : "",
              "street_name" : "GROOM",
              "sedry_number" : "4",
              "city" : "AUBURN",
              "zip" : "98001",
              "zip4" : "",
              "countryID" : 1,
              "stateID" : "WA",
              "country" : "US",
              "ContactPhoneNumber" : "8024947952",
              "created" : "2009-07-26 03:15:59",
              "street_no" : "9312",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : "LN",
              "sedry_unit" : "UNIT",
              "LocationID" : "NULL",
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2013-05-27 08:51:16",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "223vepq6pyqd6cf4q5gep29adk579k3m",
              "Customerpk" : "fpy94vf699ktzk6tetqqzxbp55urgure",
              "type" : "Billing",
              "first_name" : "User",
              "last_name" : "Synthetic",
              "BusinessName" : "OOMA",
              "ResidenceName" : "",
              "urbanization" : "Heswall",
              "street_name" : "Chapel Hill",
              "sedry_number" : "",
              "city" : "BOURNEMOUTH",
              "zip" : "BH11AA",
              "zip4" : "",
              "countryID" : 5,
              "stateID" : "",
              "country" : "GB",
              "ContactPhoneNumber" : "",
              "created" : "2017-10-25 11:09:23",
              "street_no" : "100",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-10-25 11:09:23",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "228cpdsxi9d24qbgfavzxtntug2hvw3z",
              "Customerpk" : "ec7gcukb9mnrkhrd35zn42deuxxw3wdf",
              "type" : "Shipping",
              "first_name" : "User",
              "last_name" : "Synthetic",
              "BusinessName" : "OOMA",
              "ResidenceName" : "",
              "urbanization" : "Heswall",
              "street_name" : "Chapel Hill",
              "sedry_number" : "",
              "city" : "BOURNEMOUTH",
              "zip" : "BH11AA",
              "zip4" : "",
              "countryID" : 5,
              "stateID" : "",
              "country" : "GB",
              "ContactPhoneNumber" : "",
              "created" : "2017-07-17 23:01:45",
              "street_no" : "100",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-07-17 23:01:45",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "6q4vqm3nnrm3v873774xwfr3k8qbduqn",
              "Customerpk" : "c7gvbakwm2cwf2ipqbzzu25xajjixkjg",
              "type" : "Service",
              "first_name" : "Junit",
              "last_name" : "Fakecustomer",
              "BusinessName" : "",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "Western",
              "sedry_number" : "123",
              "city" : "Mountain View",
              "zip" : "95240",
              "zip4" : "",
              "countryID" : 8,
              "stateID" : "CR",
              "country" : "ES",
              "ContactPhoneNumber" : "",
              "created" : "2017-11-30 16:14:46",
              "street_no" : "100",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : "",
              "sedry_unit" : "APT",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-11-30 16:42:17",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "75dsiiqhkjq4pvbkcmgtny3g8md68yqx",
              "Customerpk" : "ezh432wghzcwzmf8frvfd83t938ckusp",
              "type" : "Service",
              "first_name" : "",
              "last_name" : "",
              "BusinessName" : "",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "",
              "sedry_number" : "",
              "city" : "",
              "zip" : "",
              "zip4" : "",
              "countryID" : 8,
              "stateID" : "",
              "country" : "ES",
              "ContactPhoneNumber" : "",
              "created" : "2017-12-12 14:43:15",
              "street_no" : "",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-12-12 14:43:15",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "2265zz5tw6scrrrzmq8ib6viufh5y5ws",
              "Customerpk" : "snfesj23xivscjx3n9c83zeizp23j9nu",
              "type" : "Service",
              "first_name" : "Kidman",
              "last_name" : "Nicole",
              "BusinessName" : "Australian Theater Inc",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "Kangaroo Crossing",
              "sedry_number" : "",
              "city" : "Sydney",
              "zip" : "1234",
              "zip4" : "",
              "countryID" : 4,
              "stateID" : "NSW",
              "country" : "AU",
              "ContactPhoneNumber" : "",
              "created" : "2017-08-27 17:44:48",
              "street_no" : "1",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-08-27 17:44:48",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "226dzcqfht7d67ciye5qeim7epsu78zc",
              "Customerpk" : "7792gvwbdxu7ew7473te4udd9z3i93nf",
              "type" : "Service",
              "first_name" : "JunitFrance",
              "last_name" : "Fakecustomer",
              "BusinessName" : "",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "KANGAROO",
              "sedry_number" : "",
              "city" : "Sydney",
              "zip" : "1234",
              "zip4" : "",
              "countryID" : 4,
              "stateID" : "NSW",
              "country" : "AU",
              "ContactPhoneNumber" : "",
              "created" : "2017-05-18 16:27:58",
              "street_no" : "1",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : "",
              "sedry_unit" : "APT",
              "LocationID" : null,
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-07-11 16:37:40",
              "VerificationStatus" : "Unknown"
          },
          {
              "pk" : "237s5x967c4zvzr7wsfz74vwadcgumr6",
              "Customerpk" : "nxv9yf9ecpe6e944tukvdjzhpabz3mmd",
              "type" : "Shipping",
              "first_name" : "",
              "last_name" : "",
              "BusinessName" : "",
              "ResidenceName" : "",
              "urbanization" : "",
              "street_name" : "",
              "sedry_number" : "",
              "city" : "",
              "zip" : "",
              "zip4" : "",
              "countryID" : 0,
              "stateID" : "",
              "country" : "UNKNOWN",
              "ContactPhoneNumber" : "",
              "created" : "2017-03-14 23:31:35",
              "street_no" : "",
              "Salutation" : null,
              "PreDirection" : "",
              "PostDirection" : "",
              "StreetType" : null,
              "sedry_unit" : "",
              "LocationID" : "NULL",
              "GeoLat" : null,
              "GeoLon" : null,
              "Updated" : "2017-03-14 23:31:35",
              "VerificationStatus" : "Unknown"
          }
      ];

Upvotes: 0

Views: 82

Answers (2)

Andrew
Andrew

Reputation: 7545

Your original function has the correct concept. But you shouldn't be returning after your first match, right? That's what return does. You're looking to make an array, so Array#push the elements in and then return that array.

Array#filter is a great option too. In my opinion, nicer because it is cleaner code.

getAddress(arrayObj, str) {
  const addresses = []
  for(let add in arrayObj) {
    if(arrayObj[add].type === str) {
      addresses.push(arrayObj[add])
    }
  }
  return addresses;
}

getAddress(arrayObj, str) {
  return arrayObj.filter(({type}) => type === str)
}

Upvotes: 0

CRice
CRice

Reputation: 32146

If I understand correctly, this would be a good case for the .filter method of an array. Just pass in a function that returns true when the string is what you want, so something like:

getAddress(arrayObj, str) {
    return arrayObj.filter(x => x.type === str);
}

Or if you're only interested in finding the just first match instead of all matches, you can use .find instead:

// This version returns either the first object that matches the type,
// or undefined if there were no matches.
getAddress(arrayObj, str) {
    return arrayObj.find(x => x.type === str);
}

See this snippet for an example:

const address = [
{
    "pk" : "223qknzngcpcbxxx8k4iibsff3547z8t",
    "Customerpk" : "eqmemppbykdey6gwvph99f9d22nmfrav",
    "type" : "Billing",
    "first_name" : "Dhrumit",
    "last_name" : "Sheth",
    "BusinessName" : null,
    "ResidenceName" : null,
    "urbanization" : "",
    "street_name" : "",
    "sedry_number" : "",
    "city" : "",
    "zip" : "",
    "zip4" : "",
    "countryID" : 2,
    "stateID" : "",
    "country" : "CA",
    "ContactPhoneNumber" : "",
    "created" : "2011-12-20 06:13:27",
    "street_no" : "",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : "NULL",
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2016-12-16 20:35:56",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "225dk9u4s83sxvjrywtehwnyupymqu7s",
    "Customerpk" : "4fawz76u68xd9vw8qu7dgk7fk7mhw2nw",
    "type" : "Billing",
    "first_name" : "Billy",
    "last_name" : "Howard",
    "BusinessName" : "OOMACA",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "LEDUC",
    "sedry_number" : "",
    "city" : "BROSSARD",
    "zip" : "J4Y 0K7",
    "zip4" : "",
    "countryID" : 2,
    "stateID" : "QC",
    "country" : "CA",
    "ContactPhoneNumber" : "",
    "created" : "2017-04-14 23:14:55",
    "street_no" : "100",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : "BLVD",
    "sedry_unit" : "",
    "LocationID" : "NULL",
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-04-14 23:14:55",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "224f5bgmejdygvkwphpyky3d9rasq5my",
    "Customerpk" : "j2tfp56uc7sswvzcwcyqpdfqsd8msrby",
    "type" : "Billing",
    "first_name" : "User",
    "last_name" : "Synthetic",
    "BusinessName" : "OOMA",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "Calle 93 #19-55",
    "sedry_number" : "",
    "city" : "Bogotá",
    "zip" : "110221",
    "zip4" : "",
    "countryID" : 6,
    "stateID" : "",
    "country" : "CO",
    "ContactPhoneNumber" : "",
    "created" : "2018-01-15 00:48:49",
    "street_no" : "",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2018-01-15 00:48:49",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "170bf1013c6c65a1897f7e9ee774d4d1",
    "Customerpk" : "zde4z4hkgshcwh59ub4qzh6wqf4bckyy",
    "type" : "Billing",
    "first_name" : "RSH",
    "last_name" : "PARTNER",
    "BusinessName" : null,
    "ResidenceName" : null,
    "urbanization" : "",
    "street_name" : "RADIOSHACK",
    "sedry_number" : "",
    "city" : "FORT WORTH",
    "zip" : "76102",
    "zip4" : "1964",
    "countryID" : 1,
    "stateID" : "TX",
    "country" : "US",
    "ContactPhoneNumber" : "6505666641",
    "created" : "1900-01-01 00:00:00",
    "street_no" : "300",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : "CIR",
    "sedry_unit" : "",
    "LocationID" : "NULL",
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2009-10-02 00:16:37",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "222389ui49sx7mp5vx7iexhqrefdkr84",
    "Customerpk" : "9uwi4zb7bivz6fd7vj42pyjevxzu6qrt",
    "type" : "Service",
    "first_name" : "JERMAINE",
    "last_name" : "COPELAND",
    "BusinessName" : null,
    "ResidenceName" : null,
    "urbanization" : "",
    "street_name" : "GROOM",
    "sedry_number" : "4",
    "city" : "AUBURN",
    "zip" : "98001",
    "zip4" : "",
    "countryID" : 1,
    "stateID" : "WA",
    "country" : "US",
    "ContactPhoneNumber" : "8024947952",
    "created" : "2009-07-26 03:15:59",
    "street_no" : "9312",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : "LN",
    "sedry_unit" : "UNIT",
    "LocationID" : "NULL",
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2013-05-27 08:51:16",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "223vepq6pyqd6cf4q5gep29adk579k3m",
    "Customerpk" : "fpy94vf699ktzk6tetqqzxbp55urgure",
    "type" : "Billing",
    "first_name" : "User",
    "last_name" : "Synthetic",
    "BusinessName" : "OOMA",
    "ResidenceName" : "",
    "urbanization" : "Heswall",
    "street_name" : "Chapel Hill",
    "sedry_number" : "",
    "city" : "BOURNEMOUTH",
    "zip" : "BH11AA",
    "zip4" : "",
    "countryID" : 5,
    "stateID" : "",
    "country" : "GB",
    "ContactPhoneNumber" : "",
    "created" : "2017-10-25 11:09:23",
    "street_no" : "100",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-10-25 11:09:23",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "228cpdsxi9d24qbgfavzxtntug2hvw3z",
    "Customerpk" : "ec7gcukb9mnrkhrd35zn42deuxxw3wdf",
    "type" : "Shipping",
    "first_name" : "User",
    "last_name" : "Synthetic",
    "BusinessName" : "OOMA",
    "ResidenceName" : "",
    "urbanization" : "Heswall",
    "street_name" : "Chapel Hill",
    "sedry_number" : "",
    "city" : "BOURNEMOUTH",
    "zip" : "BH11AA",
    "zip4" : "",
    "countryID" : 5,
    "stateID" : "",
    "country" : "GB",
    "ContactPhoneNumber" : "",
    "created" : "2017-07-17 23:01:45",
    "street_no" : "100",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-07-17 23:01:45",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "6q4vqm3nnrm3v873774xwfr3k8qbduqn",
    "Customerpk" : "c7gvbakwm2cwf2ipqbzzu25xajjixkjg",
    "type" : "Service",
    "first_name" : "Junit",
    "last_name" : "Fakecustomer",
    "BusinessName" : "",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "Western",
    "sedry_number" : "123",
    "city" : "Mountain View",
    "zip" : "95240",
    "zip4" : "",
    "countryID" : 8,
    "stateID" : "CR",
    "country" : "ES",
    "ContactPhoneNumber" : "",
    "created" : "2017-11-30 16:14:46",
    "street_no" : "100",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : "",
    "sedry_unit" : "APT",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-11-30 16:42:17",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "75dsiiqhkjq4pvbkcmgtny3g8md68yqx",
    "Customerpk" : "ezh432wghzcwzmf8frvfd83t938ckusp",
    "type" : "Service",
    "first_name" : "",
    "last_name" : "",
    "BusinessName" : "",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "",
    "sedry_number" : "",
    "city" : "",
    "zip" : "",
    "zip4" : "",
    "countryID" : 8,
    "stateID" : "",
    "country" : "ES",
    "ContactPhoneNumber" : "",
    "created" : "2017-12-12 14:43:15",
    "street_no" : "",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-12-12 14:43:15",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "2265zz5tw6scrrrzmq8ib6viufh5y5ws",
    "Customerpk" : "snfesj23xivscjx3n9c83zeizp23j9nu",
    "type" : "Service",
    "first_name" : "Kidman",
    "last_name" : "Nicole",
    "BusinessName" : "Australian Theater Inc",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "Kangaroo Crossing",
    "sedry_number" : "",
    "city" : "Sydney",
    "zip" : "1234",
    "zip4" : "",
    "countryID" : 4,
    "stateID" : "NSW",
    "country" : "AU",
    "ContactPhoneNumber" : "",
    "created" : "2017-08-27 17:44:48",
    "street_no" : "1",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-08-27 17:44:48",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "226dzcqfht7d67ciye5qeim7epsu78zc",
    "Customerpk" : "7792gvwbdxu7ew7473te4udd9z3i93nf",
    "type" : "Service",
    "first_name" : "JunitFrance",
    "last_name" : "Fakecustomer",
    "BusinessName" : "",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "KANGAROO",
    "sedry_number" : "",
    "city" : "Sydney",
    "zip" : "1234",
    "zip4" : "",
    "countryID" : 4,
    "stateID" : "NSW",
    "country" : "AU",
    "ContactPhoneNumber" : "",
    "created" : "2017-05-18 16:27:58",
    "street_no" : "1",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : "",
    "sedry_unit" : "APT",
    "LocationID" : null,
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-07-11 16:37:40",
    "VerificationStatus" : "Unknown"
},
{
    "pk" : "237s5x967c4zvzr7wsfz74vwadcgumr6",
    "Customerpk" : "nxv9yf9ecpe6e944tukvdjzhpabz3mmd",
    "type" : "Shipping",
    "first_name" : "",
    "last_name" : "",
    "BusinessName" : "",
    "ResidenceName" : "",
    "urbanization" : "",
    "street_name" : "",
    "sedry_number" : "",
    "city" : "",
    "zip" : "",
    "zip4" : "",
    "countryID" : 0,
    "stateID" : "",
    "country" : "UNKNOWN",
    "ContactPhoneNumber" : "",
    "created" : "2017-03-14 23:31:35",
    "street_no" : "",
    "Salutation" : null,
    "PreDirection" : "",
    "PostDirection" : "",
    "StreetType" : null,
    "sedry_unit" : "",
    "LocationID" : "NULL",
    "GeoLat" : null,
    "GeoLon" : null,
    "Updated" : "2017-03-14 23:31:35",
    "VerificationStatus" : "Unknown"
}
];

function getAddress(arrayObj, str) {
    return arrayObj.filter(x => x.type === str);
}

function getSingleAddress(arrayObj, str) {
    return arrayObj.find(x => x.type === str);
}

console.log(getAddress(address, "Shipping"))
console.log(getSingleAddress(address, "Shipping"))

Upvotes: 4

Related Questions