在vue.js中访问数组对象属性

时间:2019-03-04 04:54:49

标签: javascript vuejs2

给出vue.js中的以下数组:

packageMaps: Object
    packageMap_0: Object
        Id: 16
        PackageType: "flag_list"
        ProductCode: "F-BannerBase"
    packageMap_1: Object
        Id: 82
        PackageType: "flag_list"
        ProductCode: "F-TeardropBanner"
    ....
....

...并给定值F-TeardropBanner,如何访问给定数组并返回相关的PackageType

populatePackageList(productCode) {
  let packageList = [];
  let packageType = '';
  for(let key in this.packageMaps.ProductCode) {
      if(productCode === this.packageMaps[key];
      // not sure what to do here or if I am on the right track
  }
  this.formData.packageList = Object.assign({}, this.formData.packageList, packageList);
},

Array as it appears in vue.js

1 个答案:

答案 0 :(得分:2)

如果您的响应数据很大,则将(key,value)以{product Id的形式转换为另一个对象:packageMap_x_object},如下所示,这样检索将会很快

let newPackageMaps = {
  "F-BannerBase": packageMap_0_Object
  "F-TeardropBanner":packageMap_1_Object
   ..........
   ..........
} 

populatePackageList(productCode) {
   //---------- more code----------------
  let packageMapItem = newPackageMaps[productCode]? 
      newPackageMaps[productCode]:null;
  //---------- more code ----------
}

或者简单地说,您可以使用for..in循环,

for (let packageMap in this.packageMaps) {
  if(packageMap.ProductCode == input_ProductCode ){
       // more code
       break;
   }
}