Skip to content

Instantly share code, notes, and snippets.

@pedrobritto
Created July 16, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrobritto/6c832443a4ce33b5ab5401cde34f7d2b to your computer and use it in GitHub Desktop.
Save pedrobritto/6c832443a4ce33b5ab5401cde34f7d2b to your computer and use it in GitHub Desktop.
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