Accessing array object property in vue.js

爱⌒轻易说出口 提交于 2021-02-08 20:00:13

问题


Given the following array in 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"
    ....
....

...and given the value F-TeardropBanner, how can I access the given array and return the relevant 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);
},


回答1:


If your response data is BIG in size, convert that response data to another object with (key,value) as {product Id : packageMap_x_object}, like below, thus retrieval will be fast

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 ----------
}

OR in simple you can use a for..in loop,

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


来源:https://stackoverflow.com/questions/54976830/accessing-array-object-property-in-vue-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!