Replace array of objects
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!!!
javascript arrays lodash
add a comment |
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!!!
javascript arrays lodash
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 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
– Tuhin
Nov 12 '18 at 7:53
add a comment |
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!!!
javascript arrays lodash
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!!!
javascript arrays lodash
javascript arrays lodash
asked Nov 12 '18 at 7:45
Dark Knight
84115
84115
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 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
– Tuhin
Nov 12 '18 at 7:53
add a comment |
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 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
– Tuhin
Nov 12 '18 at 7:53
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 at 7:51
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 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
– Tuhin
Nov 12 '18 at 7:53
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
– Tuhin
Nov 12 '18 at 7:53
add a comment |
4 Answers
4
active
oldest
votes
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);add a comment |
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);
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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)add a comment |
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);
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257768%2freplace-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);add a comment |
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);add a comment |
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);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);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);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);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);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);answered Nov 12 '18 at 7:52
CertainPerformance
76k143761
76k143761
add a comment |
add a comment |
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);
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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);
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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);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);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);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);answered Nov 12 '18 at 7:56
Nikhil Aggarwal
23.7k32647
23.7k32647
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Thank you I didn't test it but I made an upvote because the first answer is worked for me
– Dark Knight
Nov 12 '18 at 8:06
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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)add a comment |
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)add a comment |
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)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)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)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)edited Nov 12 '18 at 8:02
answered Nov 12 '18 at 7:54
Nitish Narang
2,948815
2,948815
add a comment |
add a comment |
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);
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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);
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
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);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);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);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);edited Nov 12 '18 at 8:12
answered Nov 12 '18 at 8:04
Mamun
25k71428
25k71428
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
Thank you I didn't test it but I made an upvote
– Dark Knight
Nov 12 '18 at 8:06
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
@DarkKnight, you are most welcome....you can test whenever you can:)
– Mamun
Nov 12 '18 at 8:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
Could you please upvote my question. Else I will be blocked by asking
– Dark Knight
Nov 12 '18 at 9:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257768%2freplace-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does it need to be in order?
– CertainPerformance
Nov 12 '18 at 7:49
@CertainPerformance No order can be anything.
– Dark Knight
Nov 12 '18 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
– Tuhin
Nov 12 '18 at 7:53