Как получить значение из каждого объекта и вставить в таблицу
К примеру у меня есть json c данными
{
"01.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"02.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"03.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"04.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"05.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"06.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"07.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"08.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"09.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"10.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"11.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"12.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}}
}
как вывести в таблицу одно и тоже значение к примеру data["01.2020"].sales.cash со всех объектов и вставить в таблицу
<table border="1" summary="">
<tr class="sum">
<th> Общая сумма продаж</th>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</table>
мой js
$.post( "data.txt", function( data ) {
console.log(data["01.2020"].sales.cash)
$items = $(".sum td").eq(0);
let i = 0;
$.each(data["01.2020"].sales.cash, function(key, val) {
$temp = $items.children().eq(i);
$temp.append( val );
i++;
});
}
};
});
Ответы (1 шт):
Автор решения: Rostyslav Kuzmovych
→ Ссылка
Можно сделать например так :
const jsonString = `{
"01.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"02.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"03.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"04.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"05.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"06.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"07.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"08.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"09.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"10.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"11.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}},
"12.2020": {"constructions": {"qty": "82288","performance": "80.7","plan": "100465","forecast": "92288","growth": "30.7"}, "sales": {"cash": "464663.45","performance": "72.7","plan": "465445","forecast": "464663.5","growth": "14.2", "average_price": "89.4"}}
}`;
const parsedJson = JSON.parse(jsonString);
const table = document.getElementById('tbl1');
Object.keys(parsedJson).forEach(k => {
var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = k;
cell2.innerHTML = parsedJson[k].sales.cash;
})
<table border="1" summary="" id="tbl1">
<tr class="sum">
<th> Дата</th>
<th> Общая сумма продаж</th>
</tr>
</table>