javascript 如何拆分对象数组并提取特定键,同时添加0以填充特定长度?

5kgi1eie  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(152)

我的对象数组看起来像这样:

[{"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "26.06.", "beginYear": "2023", "end": "03.07.", "endYear": "2023", "timestamp": 1687730400000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "19.06.", "beginYear": "2023", "end": "26.06.", "endYear": "2023", "timestamp": 1687125600000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "12.06.", "beginYear": "2023", "end": "19.06.", "endYear": "2023", "timestamp": 1686520800000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "05.06.", "beginYear": "2023", "end": "12.06.", "endYear": "2023", "timestamp": 1685916000000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "29.05.", "beginYear": "2023", "end": "05.06.", "endYear": "2023", "timestamp": 1685311200000}}]

我想通过数组Map,检查数据键是否包含超过4个数字,如果是这样,我想提取前4个数字(每个数据键在所有对象中都是相同的),并将其放入新的对象键“data”中,日期键应该看起来像原始数组的第一个项目。数据键的其余部分(在本例中为[4,2])应放入一个新数组,该数组将用0填充,直到数据的长度为4。此日期键应包含从第一个日期开始的4周内的日期。
我想修改一下,结果会是这样的:

[{
    data: [ 5, 2, 7, 2 ],
    date: {
      begin: '29.05.',
      beginYear: '2023',
      end: '05.06.',
      endYear: '2023',
      timestamp: 1685311200000
    }
  },
{
    data: [4, 2, 0, 0 ],
    date: {
      begin: '26.06.',
      beginYear: '2023',
      end: '03.07.',
      endYear: '2023',
      timestamp: 1687730400000
    }
  }
]

如果数据键包含更多的数字,该逻辑也应该适用
我试了几次,但不知何故,它不像计划的那样。

const fourStack = []

    const firstFourItems = result.slice(0, 4)
    const restItems = result.slice(4)

    const firstItem = firstFourItems[0]
    const newData = firstFourItems.map((item) => item.data.slice(0, 4))
    const newObj = {
      data: newData,
      date: {
        begin: firstItem.date.begin,
        beginYear: firstItem.date.beginYear,
        end: firstItem.date.end,
        endYear: firstItem.date.endYear,
        timestamp: firstItem.date.timestamp
      }
    }

    fourStack.push(newObj)

    restItems.forEach((item) => {
      fourStack.push({
        data: item.data.slice(0, 4),
        date: {
          begin: newObj.date.begin,
          beginYear: newObj.date.beginYear,
          end: newObj.date.end,
          endYear: newObj.date.endYear,
          timestamp: newObj.date.timestamp
        }
      })
    })

它给我的,不是我想要的结果。相反,它给了我这个:

[{"data": [[Array], [Array], [Array], [Array]], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}]
dzjeubhm

dzjeubhm1#

newData不应该是一个Map,而是firstItemdata的一个切片。通过Map,您将获得firsFourItems中所有项目的所有data
您在forEach中按下相同的date,并且您使用的data仍然是原始data密钥的前四个。它应该使用初始data的切片,并填充0,直到长度为4。
考虑:

for(var k = 0; k < result[0].data.length / 4; ++k){
    newstack.push({
        data:Array.from({...result[0].data.slice(4 * k),length:4},(v,i)=>v??0),
        date: result[k*4].date // Edit: since you want the date sorted, sort the dates then get the [k*4] of the sorted array
    })
}
slsn1g29

slsn1g292#

let arr = [{
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "03.07.",
    "beginYear": "2023",
    "end": "10.07.",
    "endYear": "2023",
    "timestamp": 1688335200000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "26.06.",
    "beginYear": "2023",
    "end": "03.07.",
    "endYear": "2023",
    "timestamp": 1687730400000
  }
}, {
  "data": [5, 2],
  "date": {
    "begin": "19.06.",
    "beginYear": "2023",
    "end": "26.06.",
    "endYear": "2023",
    "timestamp": 1687125600000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "12.06.",
    "beginYear": "2023",
    "end": "19.06.",
    "endYear": "2023",
    "timestamp": 1686520800000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "05.06.",
    "beginYear": "2023",
    "end": "12.06.",
    "endYear": "2023",
    "timestamp": 1685916000000
  }
}, {
  "data": [5],
  "date": {
    "begin": "29.05.",
    "beginYear": "2023",
    "end": "05.06.",
    "endYear": "2023",
    "timestamp": 1685311200000
  }
}];

arr.map((e) => {
  if (e.data.length > 4) {
    e.data = e.data.slice(0, 4);
  } else {
    let length = 4 - e.data.length;
    for (let i = 0; i < length; i++) {
      e.data.push(0);
    }
  }

})
console.log(arr);
xpszyzbs

xpszyzbs3#

这是一个可配置的解决方案。您可以为每个条目指定数据项(dataLength),更改每个条目中beginend日期之间的差异(daysDelay),并设置从同一初始条目获得的后续条目的begin日期之间的差异(daysOffset)。

let source = [
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}},
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "26.06.", "beginYear": "2023", "end": "03.07.", "endYear": "2023", "timestamp": 1687730400000}},
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "19.06.", "beginYear": "2023", "end": "26.06.", "endYear": "2023", "timestamp": 1687125600000}},
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "12.06.", "beginYear": "2023", "end": "19.06.", "endYear": "2023", "timestamp": 1686520800000}},
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "05.06.", "beginYear": "2023", "end": "12.06.", "endYear": "2023", "timestamp": 1685916000000}},
    {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "29.05.", "beginYear": "2023", "end": "05.06.", "endYear": "2023", "timestamp": 1685311200000}}
];

function processArray (source, dataLength, daysDelay, daysOffset) {

    function getTimezoneOffset (obj) {
        let year  = +obj.beginYear;
        let month = +obj.begin.slice(3, 5) - 1;
        let day   = +obj.begin.slice(0, 2);
        return Date.UTC(year, month, day) - obj.timestamp;
    }

    function getDDMM (date) {
        let DD = (date.getUTCDate() + 100).toString().slice(1);
        let MM = (date.getUTCMonth() + 101).toString().slice(1);
        return `${DD}.${MM}.`;
    }

    const result = [];
    const msDay = 1000 * 60 * 60 * 24;
    const msDaysDelay = daysDelay * msDay;
    const msDaysOffset = daysOffset * msDay;

    source.forEach(({data, date}) => {

        let ts = date.timestamp;

        for (let dataOffset = 0; dataOffset < data.length; dataOffset += dataLength) {
            let msTimezoneOffset = getTimezoneOffset(date);
            let dateCurrent = new Date(ts + msTimezoneOffset);
            let dateNext = new Date(ts + msTimezoneOffset + msDaysDelay);
            result.push({
                data: data.slice(dataOffset, dataOffset + dataLength),
                date: {
                    begin: getDDMM(dateCurrent),
                    beginYear: dateCurrent.getUTCFullYear(),
                    end: getDDMM(dateNext),
                    endYear: dateNext.getUTCFullYear(),
                    timestamp: ts
                }
            });
            ts += msDaysOffset;
        }

        let lastData = result[result.length - 1].data;
        while (lastData.length < dataLength) {
            lastData.push(0);
        }
    });

    return result;
}

console.log(processArray(source, 4, 7, 28));

相关问题