![](/img/trans.png)
[英]Material UI Texboxes Dynamically created form Array Disply in multi columns ReactJs
[英]Dynamically created array length in reactJS
我有一個來自API的數組:
[
{
"clicks": {
"2019-01": [
{
"clicks": "194",
"type": 0,
"user": 19
},
{
"clicks": "414",
"type": 0,
"user": 19
},
{
"clicks": "4",
"type": 90,
"user": 20
},
{
"clicks": "3",
"type": 90,
"user": 21
}
],
"2019-02": [
{
"clicks": "2",
"type": 2,
"user": 17
},
{
"clicks": "1",
"type": 1,
"user": 19
}
]
}
}
]
我想計算每個月的所有點擊次數。
我到目前為止寫的是:
const MyMonthlyClickCount = ({ record }) => {
console.log("bla", record);
var fLen, i, myMonth;
fLen = record.id.length;
myMonth = record.id;
for (i =0; i < fLen; i++) {
var ads, all, phone, z, mLen;
mLen = `record.clicks.${myMonth[i].yearmonth}`.length;
console.log("mLen:", mLen);
}
return (<StatusTextField source="record.clicks.2019-01.name" statusK="valami" />)
}
但是,mLen不能滿足我的要求。 當前,它計算字符串中的字符。
我希望mLen可以給我返回數組長度。
我怎樣才能做到這一點?
console.log的輸出:
bla {clicks: {…}, id: Array(2)}
clicks: 2019-01: (32) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
2019-02: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
__proto__: Object
id: (2) [{…}, {…}]
__proto__: Object
mLen: 21
值得添加更多數據,但您現在就開始。
let rawData = [ { "clicks": { "2019-01": [ { "clicks": "47", "id": 63, "type": 0, "user": 5 }, { "clicks": "459", "id": 5, "type": 0, "user": 5 } ], "2019-02": [ { "clicks": "0", "id": 44, "type": 0, "user": 12 } ] } } ]; const MyMonthlyClickCount = (record) => { if(Array.isArray(record) === false) record = [record]; return record.map(year => { let arrMonth = []; for(mth in year.clicks) { let m = { month: mth, clicks: year.clicks[mth].reduce((a,v) => a+=parseInt(v.clicks),0) }; arrMonth.push(m); } return arrMonth; }); }; console.log(MyMonthlyClickCount(rawData)); console.log(MyMonthlyClickCount(rawData[0]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.