简体   繁体   English

如何在Angular4 / Ionic 3中的多维数组中存储和检索数据

[英]How to Store and Retrieve Data in Multidimensional Array in Angular4 / Ionic 3

I am trying to store some values in Angular 4 (Ionic 3) and then fetch a row from the array based on a key. 我试图在Angular 4(Ionic 3)中存储一些值,然后根据键从数组中获取一行。 The key is passed into a function and the function retrieves the values with which to compute the values. 密钥被传递到函数中,该函数检索用于计算值的值。 The class property is below. class属性在下面。 I am pretty sure it is formatted right. 我很确定它的格式正确。

let chemicals = [{
    "trichlor" : [{
        "ozmul": 6854.95, 
        "volume": "x"
    }],
    "dichlor": [{
        "ozmul": 4149.03, 
        "volume": 0.9351
    }],
    "cal-hypo-48": [{
        "ozmul": 3565.44,
        "volume": 0.9352
    }],
    "cal-hypo-53": [{
        "ozmul": 3936.84,
        "volume": 0.9352
    }],
    "cal-hypo-65": [{
        "ozmul": 4828.12,
        "volume": 0.9352
    }],
    "cal-hypo-73": [{
        "ozmul": 5422.41,
        "volume": 0.9352
    }],
    "lithium-hypo": [{
        "ozmul": 2637.5,
        "volume": 0.978
    }],
    "chlorine-gas": [{
        "ozmul": 7489.4,
        "volume": "x"
    }]
}];

So basically, what I need to do is grab a value passed into a function (let's use trichlor for an example). 因此,基本上,我需要做的是获取传递给函数的值(以三氯为例)。 I then need to be able to use that value to fetch the ozmul and volume values associated with trichlor. 然后,我需要能够使用该值来获取与三氯有关的ozmul和体积值。 What is the proper way to accomplish this within a provider? 在提供程序中完成此操作的正确方法是什么? Below is an example of code from the function that uses the values: 下面是使用值的函数中的代码示例:

chemical = "trichlor"; // This is passed into the function

if (chemicals.chemical.ozmul == 'x') {
    let tempValueTwoWeight = 'Unknown';
} else {
    let tempValueTwoVolume = (tempValueTwo * chemicals.chemical.volume)
} 

Just use : 只需使用:

chemicals[0][chemical][0].ozmul

In-place of chemicals.chemical.ozmul 替代chemicals.chemical.ozmul

Some thing like this will also suit you and you will not need to hardcode paths 这样的事情也将适合您,并且您无需对路径进行硬编码

test(val:string){
  this.chemicals.forEach(data => {
    let value = data[val].find(data => data.volume === 'x');
    console.log(value);
  });
}

Have this as a method pass the val you want to search and also the data.volume parameter can be made dynamic and check 将此作为通过您要搜索的val的方法,还可以使data.volume参数动态并检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM