drno-reg
@drno-reg
см не кратко

Как правильно в JSON добавить новую серию?

Здравствуйте.

Есть JSON вида

{
    "FailedCount":[{"FailedCount_MEAS_VALUE":1,"DATETIME_CURRENT":"12:01"},
    {"FailedCount_MEAS_VALUE":0,"DATETIME_CURRENT":"12:02"},
    {"FailedCount_MEAS_VALUE":3,"DATETIME_CURRENT":"12:03"},
    {"FailedCount_MEAS_VALUE":4,"DATETIME_CURRENT":"12:04"}],
    "SucceededCount":[{"SucceededCount_MEAS_VALUE":110},
    {"SucceededCount_MEAS_VALUE":120},
    {"SucceededCount_MEAS_VALUE":130},
    {"SucceededCount_MEAS_VALUE":140}]
    }


каким образом в него правильно добавить новую "серию" AllCount, элементы являются суммой FailedCount.FailedCount_MEAS_VALUE+SucceededCount.SucceededCount_MEAS_VALUE

"AllCount":[{"AllCount_MEAS_VALUE":121},
    {"AllCount_MEAS_VALUE":120},
    {"AllCount_MEAS_VALUE":133},
    {"AllCount_MEAS_VALUE":144}]


чтобы в итоге получился следующий результат

{
    "FailedCount":[{"FailedCount_MEAS_VALUE":1,"DATETIME_CURRENT":"12:01"},
    {"FailedCount_MEAS_VALUE":0,"DATETIME_CURRENT":"12:02"},
    {"FailedCount_MEAS_VALUE":3,"DATETIME_CURRENT":"12:03"},
    {"FailedCount_MEAS_VALUE":4,"DATETIME_CURRENT":"12:04"}],
    "SucceededCount":[{"SucceededCount_MEAS_VALUE":110},
    {"SucceededCount_MEAS_VALUE":120},
    {"SucceededCount_MEAS_VALUE":130},
    {"SucceededCount_MEAS_VALUE":140}],
    "AllCount":[{"AllCount_MEAS_VALUE":121},
    {"AllCount_MEAS_VALUE":120},
    {"AllCount_MEAS_VALUE":133},
    {"AllCount_MEAS_VALUE":144}]
    }
  • Вопрос задан
  • 124 просмотра
Решения вопроса 1
eudj1n
@eudj1n
Например, вот так:

var list = {
  "FailedCount": [{
    "FailedCount_MEAS_VALUE": 1,
    "DATETIME_CURRENT": "12:01"
  }, {
    "FailedCount_MEAS_VALUE": 0,
    "DATETIME_CURRENT": "12:02"
  }, {
    "FailedCount_MEAS_VALUE": 3,
    "DATETIME_CURRENT": "12:03"
  }, {
    "FailedCount_MEAS_VALUE": 4,
    "DATETIME_CURRENT": "12:04"
  }],
  "SucceededCount": [{
    "SucceededCount_MEAS_VALUE": 110
  }, {
    "SucceededCount_MEAS_VALUE": 120
  }, {
    "SucceededCount_MEAS_VALUE": 130
  }, {
    "SucceededCount_MEAS_VALUE": 140
  }]
};

list.AllCount  = [];

for (var i = 0; i < list.FailedCount.length; i++){
    list.AllCount[i] = {};
    list.AllCount[i].AllCount_MEAS_VALUE = parseInt(list.FailedCount[i].FailedCount_MEAS_VALUE) + parseInt(list.SucceededCount[i].SucceededCount_MEAS_VALUE);
}

console.log(list);
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы