替换对象数组

时间:2018-11-12 07:45:48

标签: javascript arrays lodash

我有下面的对象数组

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
]

我有四个dayOfWeek(1,2,5,7)。现在我需要将其余三个(3,4,6)与虚拟对象({ home1: "05:30", dayOfWeek: 7, away: "09:30"})推入

现在,逻辑部分是我不知道数组中存在哪个dayOfWeek。可能只有一个,两个,三个或空白。我需要每次在该数组中推送7天。

这怎么办?请建议我最好的方法

谢谢!

4 个答案:

答案 0 :(得分:1)

一个选择是使数组中包含的日期Set到目前为止,然后从1迭代到7,push如果没有,则在该天将新对象添加到数组中包含在集合中:

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
];
const dummy = { home1: "05:30", away: "09:30" };
const days = new Set(array.map(({ dayOfWeek }) => dayOfWeek));
for (let i = 1; i <= 7; i++) {
  if (!days.has(i)) array.push({ ...dummy, dayOfWeek: i });
}
console.log(array);

我使用了Set来降低复杂度,但是我想如果您只需要7个对象,没关系,您可以使用find来代替,而无需事先创建集合

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
];
const dummy = { home1: "05:30", away: "09:30" };
for (let i = 1; i <= 7; i++) {
  if (!array.find(({ dayOfWeek }) => dayOfWeek === i)) {
    array.push({ ...dummy, dayOfWeek: i });
  }
}
console.log(array);

答案 1 :(得分:1)

您可以使用“ Array.from”并如下所示循环7次。

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
]

// sort array by day of week. Ignore this step if you are sure it will be sorted always
array.sort((a,b) => a.dayOfWeek - b.dayOfWeek)

var result = Array.from({ length: 7}
    , (_,i) => array[0].dayOfWeek == i + 1
                ? array.shift()
                : { home1: "05:30", dayOfWeek: i + 1, away: "09:30"})
                
console.log(result)

答案 2 :(得分:1)

使用 Array.reduce 创建一组现有日期。现在进行迭代,并检查是否在集合中存在一个条目,如果没有,则将该对象压入数组。

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
];
let daysSet = array.reduce((a,c) => a.add(c.dayOfWeek), new Set());
let obj = { home1: "16:30", dayOfWeek: 7, away: "09:30"};
for(let i = 1; i <=7; i++) {
  if(!daysSet.has(i)) array.push(Object.assign({}, obj, {dayOfWeek:i}));
}
console.log(array);

答案 3 :(得分:1)

您可以首先使用map()将所有 dayOfWeek 放入数组中。然后使用 for 循环通过检查数组中是否存在 dayOfWeek 来实现。

您可以尝试以下方式:

const array = [
   { home1: "05:45", dayOfWeek: 1, away: "09:30"},
   { home1: "05:15", dayOfWeek: 2, away: "09:30"},
   { home1: "17:30", dayOfWeek: 5, away: "09:30"},
   { home1: "16:30", dayOfWeek: 7, away: "09:30"}
]
let exist = array.map(d => d.dayOfWeek);

for(let i = 1; i<=7; i++){
  let dummy = { home1: "05:30", dayOfWeek: 7, away: "09:30"};
  dummy.dayOfWeek = i;
  if(!exist.includes(i))
   array.splice(i-1, 0, dummy);
}
console.log(array);