迭代数组对象Javascript

时间:2018-01-19 22:28:03

标签: javascript arrays reactjs

我正在尝试遍历数组对象。我想只返回整个对象,只有匹配类型的记录与我输入的字符串匹配'类型'当密钥匹配我在函数中传递的密钥时键入对象,在这种情况下是函数计费。我在react中编写一个javascript函数,它接受一个数组和字符串作为参数。 我无法正确匹配密钥。

如果str是' Billing',则应返回所有类型为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.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"
          }
      ];

2 个答案:

答案 0 :(得分:4)

如果我理解正确,这对于数组的.filter方法来说是个好例子。只需传入一个函数,当字符串符合您的要求时返回true,如下所示:

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

或者,如果您只想找到第一场比赛而不是所有比赛,那么您可以使用.find代替:

// 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);
}

请参阅此代码段以获取示例:



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"))




答案 1 :(得分:0)

您的原始功能具有正确的概念。但是你在第一场比赛后不应该回来,对吗?这是else if (donorGender != "Male" && donorGender != "Female" && donorGender != "Trans Male" && donorGender != "Trans Female" && donorGender != "Queer" && donorGender != "Different") 的作用。你正在寻找一个数组,所以<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks> </PropertyGroup> 中的元素然后返回那个数组。

return也是一个不错的选择。在我看来,更好,因为它是更清晰的代码。

Array#push