gpt4 book ai didi

javascript - 对象数组值的 JSON 数学运算

转载 作者:行者123 更新时间:2023-11-28 18:02:46 25 4
gpt4 key购买 nike

我正在尝试将对象数组中的两个组件相乘

{
"rx": {
"vials": [
{
"description": "Rx 1",
"strength": 100,
"form": "ML",
"pkg_size": 10,
"case_size": 1,
"total_units": strength * pkg_size,
"ndc": "12345-1234-12",
"covered": true
}
]
}
}

但是如果我做类似的事情

$( "#demo" ).html( rx.vials[0].total_units );

我什么也没得到或NaN

我做错了什么?

最佳答案

你可以尝试这样的事情:

完成计算后,您可以将 total_units 属性添加到 vias[0] 中。

var jsonObj = {
"rx": {
"vials": [
{
"description": "Rx 1",
"strength": 100,
"form": "ML",
"pkg_size": 10,
"case_size": 1,
"ndc": "12345-1234-12",
"covered": true
}
]
}
};

jsonObj.rx.vials[0].total_units = jsonObj.rx.vials[0].strength * jsonObj.rx.vials[0].pkg_size;

console.log(jsonObj.rx.vials[0].total_units);

关于javascript - 对象数组值的 JSON 数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43234401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com