问题
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