Javascript map obejct in array of objects如果条件不满足则向数组中添加新对象

egdjgwm8  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(88)

不知道为什么我在javascript和对象方法上挣扎得这么厉害,但这让我的大脑很受伤。我正在接收后端数据,需要将嵌套对象数组中的值与日期数组中的值进行比较,如果不匹配,则添加一个对象。
数据:

[{
  _id: "6205a8313fe12d6b4ec354c4",
  fullName: "Johnny Cochrane",
  contracts: {
    type: "Quarterly",
    hours: 120,
  },
  client: "6205a8313fe12d6b4ec354c4",
  hours: [{
    date: "2023-5",
    count: 6,
  }, {
    date: "2023-6",
    count: 2,
  }],
}, {
  _id: "6217c1b73fe12d6b4ec3550e",
  fullName: "Sheila Thompson",
  contracts: {
    type: "Quarterly",
    hours: 140,
  },
  client: "6217c1b73fe12d6b4ec3550e",
  hours: [{
    date: "2023-4",
    count: 5.5,
  }, {
    date: "2023-6",
    count: 2,
  }],
}]

设置表头比较和准备的日期:

const qtrs = [{qtr: 1, mths: [1,2,3]}, {qtr: 2, mths: [4,5,6]}, {qtr: 3, mths: [7,8,9]}, {qtr: 4, mths: [10,11,12]} ]

const currentMth = new Date().getMonth() + 1

const currentQtr = qtrs.filter(({mths}) => mths.includes(currentMth))

const compDates = currentQtr[0].mths.map(i => new Date(new Date(new Date().setMonth(i)).setHours(0, 0, 0, 0)).toISOString())

我正在努力处理表中的数据的部分:

const adjust = data.forEach(i => {
  i.hours.forEach(item => {
  console.log("item1: ",item)
  const interim = new Date(item.date).getMonth() + 1
  console.log(interim)
  item.date =  new Date(new Date(new Date().setMonth(interim)).setHours(0, 0, 0, 0)).toISOString()
  console.log(item.date)

//Steps I've tried- 1st:
cursor = 0
Object.entries(item).map(obj => {console.log(obj); compDates[cursor] && compDates[cursor] == obj.date ? {...obj} :{count: 0, date: obj.date, total: obj.total}});

//2nd try
Object.values(item).map(x => (compDates.find(y => y === x.date) ? {...x}: {count: 0, date: x.date, total: x.total}))

//3rd
if (compDates.find(y => y === item.date)) {
    item = item
  } else {
   item = {count: 0, date: item.date, total: item.total}
  } 

})})

我尝试了Object.values、Object.keys沿着Object.entries,并更改了函数。我知道其中任何一个应该能够工作,我只是不知道我错过了什么,以获得正确的回应。
我对这些数据的理想输出是:
比较日期:[“2023-05-16T07:00:00.258Z”,“2023-06-16T07:00:00.258Z”,“2023-07-16T07:00:00.258Z”]

[{
  _id: "6205a8313fe12d6b4ec354c4",
  fullName: "Johnny Cochrane",
  contracts: {
    type: "Quarterly",
    hours: 120,
  },
  client: "6205a8313fe12d6b4ec354c4",
  hours: [{
    date: "2023-4", // this does not need to be changed back from the ISO date in the comparison stage
    count: 0,
  }, {
    date: "2023-5",
    count: 6,
  }, {
    date: "2023-6",
    count: 2,
  }],
}, {
  _id: "6217c1b73fe12d6b4ec3550e",
  fullName: "Sheila Thompson",
  contracts: {
    type: "Quarterly",
    hours: 140,
  },
  client: "6217c1b73fe12d6b4ec3550e",
  hours: [{
    date: "2023-4",
    count: 5.5,
  }, {
    date: "2023-5", // again does not need to be changed back
    count: 0,
  }, {
    date: "2023-6",
    count: 2,
  }],
}]
y3bcpkx1

y3bcpkx11#

有几件事。第一个是我在如何与约会对象互动方面的疏忽。为了让我的非编程思维更容易,我没有将初始qtrs-mths数组调整为0,因此进行了调整,还设定了一个实际的日子(最初我认为这不会是一个问题,因为我只是在一个新的日期()上设置一个月,每次都会反映当前的一天是什么(显然没有想透这一点,因为任何一个月与31会搞砸,并在最后,仍然造成问题)。因此,设置新日期:

const qtrs = [{qtr: 1, mths: [0,1,2]}, {qtr: 2, mths: [3,4,5]}, {qtr: 3, mths: [6,7,8]}, {qtr: 4, mths: [9,10,11]} ]

const currentMth = new Date().getMonth()
console.log(currentMth)

const currentQtr = qtrs.filter(({mths}) => mths.includes(currentMth))
console.log(currentQtr)

const compDates = currentQtr[0].mths.map(i => new Date(new Date(new Date(new Date().setMonth(i)).setDate(1)).setHours(0,0,0,0)).toISOString())

console.log("comp", compDates)

然后,在@Peterrabbit的提醒下,我放弃了我正在做的事情,重新开始,下面的内容作为工作结果。主要的区别是设置一个空对象来赋值,然后推入小时数组,并且还需要查看有关Array.find()和Array.filter()如何返回结果的文档。find()返回第一个与条件匹配的项,而filter可用于执行相反的操作,使用.includes可使此操作变得简单。并不是说Array.find()不能用来做这件事,但至少对我来说这似乎更简单。从MDN和这个SO answer

data.forEach(i => {
let newObj = {} 

let itemsDate = i.hours.map(x => { let adjustMth = new Date(x.date).getMonth() + 1; return (new Date(new Date(new Date(new Date().setMonth(adjustMth)).setDate(1)).setHours(0,0,0,0)).toISOString())})
console.log("itemsDate", itemsDate)

let getTotal = i.hours[0].total
console.log(getTotal)

let result = compDates.filter(y => !itemsDate.includes(y))
console.log("result:", result)

if (result.length) {
    result.forEach(d => {
        newObj = {
        count: 0,
      date: d,
      total: getTotal
    };
    i.hours.push(newObj)
   } )
} else {
    console.log(false)
}

i.hours.sort(function(a,b){
  return new Date(a.date) - new Date(b.date);
});

console.log(i)
return i 
 })
 
 console.log("adjust: ", data)

无论如何,它都不是优雅的javascript,但我的大脑显然需要一步一步地把它拼出来。下面是控制台日志/调整数据对象的结果:

"current month: ", 5
"current quarter: ", [{
  mths: [3, 4, 5],
  qtr: 2
}]
"comp", ["2023-04-01T07:00:00.000Z", "2023-05-01T07:00:00.000Z", "2023-06-01T07:00:00.000Z"]
"itemsDate", ["2023-05-01T07:00:00.000Z", "2023-06-01T07:00:00.000Z"]
40
"result:", ["2023-04-01T07:00:00.000Z"]
"itemsDate", ["2023-04-01T07:00:00.000Z", "2023-06-01T07:00:00.000Z"]
46.67
"result:", ["2023-05-01T07:00:00.000Z"]
"adjust: ", [{
  _id: "6205a8313fe12d6b4ec354c4",
  client: "6205a8313fe12d6b4ec354c4",
  contracts: {
    monthHours: 40,
    total: 120,
    type: "Quarterly"
  },
  fullName: "Johnny Cochrane",
  hours: [{
  count: 0,
  date: "2023-04-01T07:00:00.000Z",
  total: 40
}, {
  count: 6,
  date: "2023-5",
  total: 40
}, {
  count: 2,
  date: "2023-6",
  total: 40
}]
}, {
  _id: "6217c1b73fe12d6b4ec3550e",
  client: "6217c1b73fe12d6b4ec3550e",
  contracts: {
    monthHours: 46.67,
    total: 140,
    type: "Quarterly"
  },
  fullName: "Sheila Thompson",
  hours: [{
  count: 5.5,
  date: "2023-4",
  total: 46.67
}, {
  count: 0,
  date: "2023-05-01T07:00:00.000Z",
  total: 46.67
}, {
  count: 2,
  date: "2023-6",
  total: 46.67
}]
}]

相关问题