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

标签 javascript arrays json object math

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

{
  "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/

相关文章:

c - 了解二维数组中行的 c 指针

java - Android 中的搜索按钮功能

javascript - 如何使用复选框向数组添加/删除项目?

javascript - 正确识别区域设置日期字符串

javascript - Twitter Bootstrap Carousel 跳过问题

javascript - 吊装有什么好处吗?

c - 为什么这个 C 代码有效?

javascript - 错误: [ngRepeat:iexp] Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '(name,surname)in controller.students'

java - 使用数据文件进行 Junit 测试

javascript - 降落区 : Uncaught Error: No URL provided