【问题标题】:How do I convert an array to an object with different keys on the first and last array elements?如何将数组转换为在第一个和最后一个数组元素上具有不同键的对象?
【发布时间】:2020-06-19 09:20:55
【问题描述】:

这是我迄今为止尝试过的。

除了我不知道如何更改第一个和最后一个日期键之外,还有哪些可行?

 const dates: any = [
    '2020-06-24',
    '2020-06-25',
    '2020-06-26',
    '2020-06-27',
    '2020-06-28',
    '2020-06-29',
    '2020-06-30',
  ];

  const dateArrayToObject = () => {
    const dateObject = dates.reduce(
      (acc: string, date: string) =>
        Object.assign(acc, {
          [date]: { selected: true, marked: true },
        }),
      {}
    );

    return dateObject;
  };

【问题讨论】:

  • 您的预期结果是什么?
  • 第一个[date]:{startingDay:true,marked:true}和最后一个第一个[date]:{endingDay:true,marked:true},

标签: javascript arrays object javascript-objects reduce


【解决方案1】:

我想这就是你需要的。

const dates = [
    '2020-06-24',
    '2020-06-25',
    '2020-06-26',
    '2020-06-27',
    '2020-06-28',
    '2020-06-29',
    '2020-06-30',
  ];

var res= dates.reduce((acc, v, index, arr)=>{
  if(index === 0){
    acc = [...acc, {[v] : {startingDay: true, marked: true}} ]
  }else if(index === arr.length-1){
    acc = [...acc, {[v] : {ending: true, marked: true}} ]
  }else{
    acc = [...acc, {[v] : {selected: true, marked: true}} ]
  }
  return acc
},[])
console.log(res)

更新:如果要将结果保留为对象而不是数组,可以这样做:

 const dates = [
        '2020-06-24',
        '2020-06-25',
        '2020-06-26',
        '2020-06-27',
        '2020-06-28',
        '2020-06-29',
        '2020-06-30',
      ];

    var res= dates.reduce((acc, v, index, arr)=>{
     
           acc = index === 0? {...acc, ...{[v] : {startingDay: true, marked: true}} }
              : index === arr.length-1 ? acc = {...acc, ...{[v] : {ending: true, marked: true}} }
              : acc = {...acc, ...{[v] : {selected: true, marked: true}} }
      return acc
    },{})
    console.log(res)

【讨论】:

  • 虽然我确实需要将它保留为对象而不是数组,这是一个小问题
  • 您可以将{}代替[]作为acc并相应地生成代码
【解决方案2】:

在reduce方法中使用第三个参数i(索引)。

 const dates = [
    '2020-06-24',
    '2020-06-25',
    '2020-06-26',
    '2020-06-27',
    '2020-06-28',
    '2020-06-29',
    '2020-06-30',
  ];

  const dateArrayToObject = () => {
    const dateObject = dates.reduce(
      (acc, date, i) =>
        Object.assign(acc, {
          [date]: { selected: true, marked: true, 
            first: i === 0,
            last: i === dates.length - 1 
            },
        }),
      {}
    );

    return dateObject;
  };
  
  console.log(dateArrayToObject())

【讨论】:

    【解决方案3】:

    使用map 操作,您可以使用一些额外的道具转换每个对象,例如firstElementlastElement,并在返回新对象数组时保留其余元素。

    const dates = [
        '2020-06-24',
        '2020-06-25',
        '2020-06-26',
        '2020-06-27',
        '2020-06-28',
        '2020-06-29',
        '2020-06-30',
    ];
    
    dates.map((value, index) => {
                const obj = {
                    selected: true,
                    marked: true
                };
    
                if (index == 0) {
                    obj.firstElement = true;
                    return obj;
                } 
                else if (index == dates.length - 1) {
                    obj.lastElement = true;
                    return obj;
                } 
                else {
                    return obj;
                }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 2017-07-17
      • 2022-11-17
      • 2013-12-05
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多