从数组中的特定位置添加另一个值?

时间:2017-09-19 07:17:24

标签: javascript typescript

有这么多个月:

let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',];

有这样一个功能:

    function headerCellRendererFunc(params) {
        var eHeader = document.createElement('span');
        var eTitle = document.createTextNode(params.colDef.headerName + ' ' + year);
        eHeader.appendChild(eTitle);
        return eHeader;
    }

在出路的时候我得到了:

  • July'17
  • August'17
  • September'17 e.t.c ...

我需要从1月到 + 1

会发生什么:

  • 7月''8月17日''9月17日''十月十七日'十一月十七日'十二月十七日'17 1月18日'2月18日''3月18日''5月18日''18''18

如何在函数中创建条件?

    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));

4 个答案:

答案 0 :(得分:1)

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));

<强>更新

我把headerCellRendererFunc带到了地图之外;你需要将年份绑定到它,因为ColDef.headerCellRenderer将参数传递给函数。

对于year,您只需要检查1月是否是第一个月,或者当前月份索引是否小于1月份的索引。如果是,请将其设置为this.fy。如果不是,this.fy + 1

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 :(得分:0)

试试这个:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以从1月份获取月份数组,并使用余数运算符取值。对于这一年,您可以将月份除以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));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

要制作条件,您需要将月份映射到值...

DF2