Created
July 16, 2019 19:23
-
-
Save pedrobritto/6c832443a4ce33b5ab5401cde34f7d2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const defaultArr = [ | |
{ day: 1, from: undefined, to: undefined }, | |
{ day: 2, from: undefined, to: undefined }, | |
{ day: 3, from: undefined, to: undefined }, | |
{ day: 4, from: undefined, to: undefined }, | |
{ day: 5, from: undefined, to: undefined }, | |
{ day: 6, from: undefined, to: undefined }, | |
{ day: 7, from: undefined, to: undefined } | |
]; | |
const customArr = [ | |
{ day: 1, from: "12:00", to: "15:00" }, | |
{ day: 2, from: "13:00", to: "15:00" } | |
]; | |
function objectMerge(defaultArr, customArr) { | |
const finalArr = []; | |
const customDayValues = []; | |
for (item of customArr) { | |
customDayValues.push(item.day); | |
} | |
for (item of defaultArr) { | |
if (customDayValues.includes(item.day)) { | |
customArr.map(customItem => { | |
if (customItem.day === item.day) { | |
finalArr.push(customItem); | |
} | |
}); | |
} else { | |
finalArr.push(item); | |
} | |
} | |
return finalArr; | |
} | |
const finalArr = objectMerge(defaultArr, customArr); | |
console.log(finalArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment