遍历数组对象Javascript

遍历数组对象Javascript,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我试图遍历数组对象。我只想返回整个对象,其中记录的类型与我输入的字符串相匹配,如果它与对象中的“type”键相匹配,那么当键与我在函数中传递的键相匹配时(在本例中为billing)。我正在react中编写一个javascript函数,它以数组和字符串作为参数。 我无法正确匹配钥匙 如果str为“Billing”,则应返回类型为Billing的所有记录 getAddress(arrayObj, str) { for(let add in arrayObj) {

我试图遍历数组对象。我只想返回整个对象,其中记录的类型与我输入的字符串相匹配,如果它与对象中的“type”键相匹配,那么当键与我在函数中传递的键相匹配时(在本例中为billing)。我正在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"
          }
      ];

如果我理解正确,这将是数组的
.filter
方法的一个好例子。只需传入一个函数,该函数在字符串为所需字符串时返回true,如下所示:

getAddress(arrayObj, str) {
    return arrayObj.filter(x => x.type === str);
}
或者,如果您只想查找第一个匹配项而不是所有匹配项,则可以使用
。查找

// 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地址=[
{
“主键”:“223qknzngcpcbxxx8k4iibsff3547z8t”,
“Customerpk”:“EQMempBPYKDEY6GWVPH99F9D22NMFRAV”,
“类型”:“账单”,
“名字”:“Dhrumit”,
“姓”:“谢特”,
“BusinessName”:空,
“住宅名称”:空,
“城市化”:“,
“街道名称”:“,
“sedry_编号”:“,
“城市”:“,
“zip”:“,
“zip4”:“,
“countryID”:2,
“stateID”:“stateID”,
“国家”:“CA”,
“联系人电话号码”:“,
“已创建”:“2011-12-2006:13:27”,
“街道号”:“街道号”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“StreetType”:空,
“sedry_单位”:“,
“LocationID”:“NULL”,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2016-12-16 20:35:56”,
“验证状态”:“未知”
},
{
“pk”:“225dk9u4s83sxvjrywtehwnyupymqu7s”,
“客户方”:“4fawz76u68xd9vw8qu7dgk7fk7mhw2nw”,
“类型”:“账单”,
“名字”:“比利”,
“姓”:“霍华德”,
“商业名称”:“OOMACA”,
“住宅名称”:“,
“城市化”:“,
“街道名称”:“LEDUC”,
“sedry_编号”:“,
“城市”:“布罗萨尔”,
“zip”:“J4Y 0K7”,
“zip4”:“,
“countryID”:2,
“stateID”:“QC”,
“国家”:“CA”,
“联系人电话号码”:“,
“已创建”:“2017-04-14 23:14:55”,
“街道号”:“100”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“街道类型”:“大道”,
“sedry_单位”:“,
“LocationID”:“NULL”,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2017-04-14 23:14:55”,
“验证状态”:“未知”
},
{
“主键”:“224F5BGMEJDYGVKWPHYKY3D9RASQ5MY”,
“Customerpk”:“j2tfp56uc7sswvzcwcyqpdfqsd8msrby”,
“类型”:“账单”,
“名字”:“用户”,
“姓氏”:“合成”,
“商业名称”:“OOMA”,
“住宅名称”:“,
“城市化”:“,
“街道名称”:“Calle 93#19-55”,
“sedry_编号”:“,
“城市”:“波哥大”,
“zip”:“110221”,
“zip4”:“,
“countryID”:6,
“stateID”:“stateID”,
“国家”:“公司”,
“联系人电话号码”:“,
“已创建”:“2018-01-15 00:48:49”,
“街道号”:“街道号”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“StreetType”:空,
“sedry_单位”:“,
“LocationID”:空,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2018-01-15 00:48:49”,
“验证状态”:“未知”
},
{
“主键”:“170BF1013C6C65A1897F7E9EE774DD1”,
“客户方”:“zde4z4hkgshcwh59ub4qzh6wqf4bckyy”,
“类型”:“账单”,
“名字”:“RSH”,
“姓氏”:“合伙人”,
“BusinessName”:空,
“住宅名称”:空,
“城市化”:“,
“街道名称”:“无线电黑客”,
“sedry_编号”:“,
“城市”:“沃思堡”,
“zip”:“76102”,
“zip4”:“1964年”,
“countryID”:1,
“stateID”:“TX”,
“国家”:“美国”,
“联系人电话号码”:“650566641”,
“已创建”:“1900-01-01 00:00:00”,
“街道号”:“300”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“StreetType”:“CIR”,
“sedry_单位”:“,
“LocationID”:“NULL”,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2009-10-02 00:16:37”,
“验证状态”:“未知”
},
{
“主键”:“222389ui49sx7mp5vx7iexhqrefdkr84”,
“Customerpk”:“9uwi4zb7bivz6fd7vj42pyjevxzu6qrt”,
“类型”:“服务”,
“名字”:“杰梅因”,
“姓氏”:“科普兰”,
“BusinessName”:空,
“住宅名称”:空,
“城市化”:“,
“街道名称”:“新郎”,
“sedry_编号”:“4”,
“城市”:“奥本”,
“zip”:“98001”,
“zip4”:“,
“countryID”:1,
“stateID”:“WA”,
“国家”:“美国”,
“联系人电话号码”:“8024947952”,
“已创建”:“2009-07-2603:15:59”,
“街道号”:“9312”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“StreetType”:“LN”,
“sedry_单位”:“单位”,
“LocationID”:“NULL”,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2013-05-27 08:51:16”,
“验证状态”:“未知”
},
{
“主键”:“223vepq6pyqd6cf4q5gep29adk579k3m”,
“Customerpk”:“FPY94VF699KTZK6ETQZBP55urgure”,
“类型”:“账单”,
“名字”:“用户”,
“姓氏”:“合成”,
“商业名称”:“OOMA”,
“住宅名称”:“,
“城市化”:“Heswall”,
“街道名称”:“教堂山”,
“sedry_编号”:“,
“城市”:“伯恩茅斯”,
“zip”:“BH11AA”,
“zip4”:“,
“countryID”:5,
“stateID”:“stateID”,
“国家”:“GB”,
“联系人电话号码”:“,
“已创建”:“2017-10-25 11:09:23”,
“街道号”:“100”,
“称呼”:空,
“预述”:“预述”,
“后置方向”:“后置方向”,
“StreetType”:空,
“sedry_单位”:“,
“LocationID”:空,
“GeoLat”:空,
“GeoLon”:空,
“更新”:“2017-10-25 11:09:23”,
“验证状态”:“未知”
},
{
“主键”:“228cpdsxi9d24qbgfavzxtntug2hvw3z”,
“Customerpk”:“ec7gcukb9mnrkhrd35zn42deuxxw3wdf”,
“类型”:
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)
}