From a specific place in the array, add another value?
有这样一系列的月份:
1 | let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',]; |
有这样一个功能:
1 2 3 4 5 6 | function headerCellRendererFunc(params) { var eHeader = document.createElement('span'); var eTitle = document.createTextNode(params.colDef.headerName + ' ' + year); eHeader.appendChild(eTitle); return eHeader; } |
在出去的路上我得到:
- 七月17日
- 八月十七日
- 九月17日E.T.C.
我需要从一月份到一年有+1
会发生什么事呢?
- 七月十七日八月十七日九月十七日十月十七日十一月十七日十二月十七日1月18日2月18日3月18日4月18日5月18日6月18日
如何在函数中生成条件?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | months.map((month, index) => { var year = this.fy; function headerCellRendererFunc(params) { var eHeader = document.createElement('span'); var eTitle = document.createTextNode(params.colDef.headerName + ' ' + year); eHeader.appendChild(eTitle); return eHeader; } return <ColDef>{ headerName: month, headerCellRenderer: headerCellRendererFunc, field: `data.${month}`, editable: isEditable, valueGetter: cellMonthValueGetter(month, index), cellFormatter: cellMonthValueFormatter, newValueHandler: cellMonthNewValueHandler(month), width: 100, }; }).forEach(colDef => colDefs.push(colDef)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | const months = [ 'July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June', ]; /** * Sets the year of the month and returns the new array of months. * @param {number} year - The two-digit year. * @param {string[]} arrMonths - The array of string months in order. * @returns string[] */ function setYear(year, arrMonths) { return arrMonths.map((month, i) => { // Increment the year if this is not the first pass and the month is January. if (i !== 0 && month.toLowerCase() === 'january') { year++; } return `${month} '${year}`; }); } console.log(setYear(17, months)); |
更新
我把
对于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function headerCellRendererFunc(year, params) { const eHeader = document.createElement('span'); eHeader.innerText = `${params.colDef.headerName} ${year}`; return eHeader; } const januaryIndex = months.indexOf('January'); months.map((month, index, arr) => { const year = januaryIndex === 0 || index < januaryIndex ? this.fy : this.fy + 1; return <ColDef>{ headerName: month, headerCellRenderer: headerCellRendererFunc.bind(null, year), field: `data.${month}`, editable: isEditable, valueGetter: cellMonthValueGetter(month, index), cellFormatter: cellMonthValueFormatter, newValueHandler: cellMonthNewValueHandler(month), width: 100, }; }).forEach((colDef) => colDefs.push(colDef)); |
要设置条件,您需要将月份映射为值…
1 2 3 4 5 6 | let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',]; let monthMap = {Janurary:1,Feburary:2,March:3,April:4,May:5,June:6,July:7,August:8,September:9,October:10,November:11,December:12} if(monthMap[month] >= 1 && monthMap[month] <= 6) //format... |
您可以从1月份开始使用Months数组,并使用Remains运算符获取该值。对于这一年,你可以把月份除以12,然后取一个不相等的值。
1 2 3 4 5 6 7 8 9 10 11 12 | function getValues(startMonth, startYear, count) { var months = ['December', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November'], i, result = []; for (i = 0; i < count; i++) { result.push(months[(startMonth + i) % 12] + ' ' + Math.floor(startYear + (startMonth + i - 1) / 12)) } return result; } console.log(getValues(7, 2017, 12)); |
1 | .as-console-wrapper { max-height: 100% !important; top: 0; } |
试试这个:
1 2 3 4 | var months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June']; var januaryIndex = months.indexOf("January"); var finalMonths = months.map((month, index) => (index < januaryIndex ? month +"'17" : month +"'18")); console.log(finalMonths); |