4

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

Array as it appears in vue.js

2
  • 1
    Can you share the proper data array? Commented Mar 4, 2019 at 4:57
  • I only have the front end data -the JSON is interpreted by vue.js as represented above. Commented Mar 4, 2019 at 5:00

1 Answer 1

2

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;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.