给出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);
},
答案 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;
}
}