使用Json嵌套键迭代和执行算术运算的函数

时间:2018-04-18 06:53:32

标签: javascript json vuejs2

我需要创建一个函数来迭代json对象'menu',返回一些json嵌套键。使用其中三个键,我需要实现一些简单的算术运算,如下所示: 首先我需要添加所有potatoes,然后我应该将所有tomatoes乘以此beef,然后将结果除以carrots。请注意,所有键都等于数字,因此计算类似于:(8 * 2.083488 + 40 * 1.5)/(180 + 180)

注意我的对象是计算属性,应该在vue模板中显示(但我认为这对我来说不是最重要的事情)

提前致谢,如果您需要更多解释,我就在附近

这是我的功能:

   myFunc: function() {
      var array = JSON.parse(totalMenu);
      array.forEach(function(object) {
       console.log(object.carrots);
      });
    }

这是JSON(包含在totalMenu对象中):

      {
      "foo32":[
        {
            "dates":[
                {
                    "date":"2018-04-02",
                    "foo":[
                        {
                            "foo78":[
                                {
                                    "fooId":1
                                }
                            ],
                            "foo54":2.636000,
                            "beef":2.083488,
                            "foo09":0,
                            "foo":"",
                            "potatoes":8,
                            "foo90":0,
                            "foo55":0,
                            "fooLabel":"360 cheese nana "
                        }
                    ]
                },
                {
                    "date":"2018-04-03",
                    "fooReceipe":[
                        {
                            "foo65":0,
                            "fooID":"FX0000010000984",
                            "foo21":0,
                            "foo74":1,
                            "foo75":[
                                {
                                    "foo89":"NOR",
                                    "foo67":1
                                }
                            ],
                            "foo34":2.636000,
                            "beef":1.50,
                            "foo58":0,
                            "foo5":"",
                            "potatoes":40,
                            "foo2":0,
                            "foo":0,
                            "familleLabel":"Plate for nowhere "
                        }
                    ]
                }
            ],
            "familyLabel":"Starter",
            "fammilyreceipe":13,
            "family":7
        }
    ],
    "foo23":1002,
    "totalfoo":[
        {
            "foo4":0,
            "foo2":"2018-04-02",
            "foo":0,
            "carrots":180
        },
        {
            "foo3":0,
            "foo2":"2018-04-03",
            "foo":0,
            "carrots":180
        }
    ]
}

0 个答案:

没有答案