字符串 转换 数组

将字符串转换成自定义数组结构:

changeData() {
        const { timesettings } = this.state;
        let workTimes = [];
        for (var i in timesettings) {
            let newtimeslots = [];
            timesettings[i].map((elem, index) => {
                let newtimeslot = {};
                newtimeslot["id"] = index;
                newtimeslot["starttime"] = elem.starttime;
                newtimeslot["endtime"] = elem.endtime;
                newtimeslots.push(newtimeslot);
            });
            let timeslots = {};
            timeslots["type"] = i;
            _timeslots["timeslots"] = newtimeslots;
            workTimes.push(timeslots);
            let arr = workTimes;
            let newworkTimes = [];
            arr.map((j, k) => {
                let workTimes = {};
                _workTimes["id"] = k;
                _workTimes["type"] = j.type;
                _workTimes["timeslots"] = j.timeslots;
                newworkTimes.push(workTimes);
            });
            workTimes = newworkTimes;
        }
        this.setState({
            timesettings: workTimes
        });
    }

图例:

重构数据前:

image.png

重构数据后:

image.png

日记本

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

赞赏支持
被以下专题收入,发现更多相似内容