简体   繁体   English

遍历数组对象的Javascript

[英]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. 我在React中编写一个javascript函数,该函数将数组和字符串作为参数。 I am not able to match the keys correctly. 我无法正确匹配按键。

If str is 'Billing', it should return all the records with type Billing. 如果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 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"
          }
      ];

If I understand correctly, this would be a good case for the .filter method of an array. 如果我理解正确,那么这对于数组的.filter方法将是一个很好的例子。 Just pass in a function that returns true when the string is what you want, so something like: 只需传入一个当您想要的字符串为真时返回true的函数,例如:

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: 或者,如果您只想查找仅第一个匹配项而不是所有匹配项,则可以改用.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);
}

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

Your original function has the correct concept. 您的原始功能具有正确的概念。 But you shouldn't be returning after your first match, right? 但是您不应该在第一场比赛后回来,对吗? That's what return does. 那就是return You're looking to make an array, so Array#push the elements in and then return that array. 您正在寻找一个数组,因此Array#push元素然后返回该数组。

Array#filter is a great option too. Array#filter也是一个不错的选择。 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM