0

I have below array of objects

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"}
]

I have four dayOfWeek (1,2,5,7). Now I need to push the remaining three (3,4,6) with the dummy object ({ home1: "05:30", dayOfWeek: 7, away: "09:30"})

Now the logical part is I don't know which dayOfWeek are present in the array. There may be only one, two, three or blank. I need to push 7 days in that array every time.

How this can be done? Please suggest me the best way

Thank you!!!

3
  • Does it need to be in order? Commented Nov 12, 2018 at 7:49
  • @CertainPerformance No order can be anything. Commented Nov 12, 2018 at 7:51
  • you can loop through current array and store daysOfWeek in an array then loop the other 7 days dummy array and check if its in the previous array then do not need to insert that days. hopes you get that Commented Nov 12, 2018 at 7:53

4 Answers 4

1

One option would be to make a Set of the days contained in the array so far, then iterate from 1 to 7, pushing a new object to the array with that day if it's not contained in the set:

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);

I used a Set to reduce complexity, but I suppose if you only ever need 7 objects, it doesn't matter much, you could use find instead without creating a collection beforehand

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);

Sign up to request clarification or add additional context in comments.

Comments

1

Use Array.reduce to create a set of existing days. Now iterate and check whether there is an entry in set, if not push the object in array.

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);

2 Comments

Thank you I didn't test it but I made an upvote because the first answer is worked for me
Could you please upvote my question. Else I will be blocked by asking
1

You can use "Array.from" and loop 7 times like below.

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)

Comments

1

You can first get all the dayOfWeek present in a array with map(). Then use for loop to insert the dayOfWeek by checking whether that is present in the array or not.

You can try the following way:

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);

1 Comment

Could you please upvote my question. Else I will be blocked by asking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.