Merge two objects but only existing properties

Assuming you only want enumerable properties, this is easily done with Object.keys and in (or hasOwnProperty): Object.keys(obj2).forEach(function(key) { if (key in obj1) { // or obj1.hasOwnProperty(key) obj1[key] = obj2[key]; } }); Example: var obj1 = { “name”: “”, “age”: “” }; var obj2 = { “name”: “Leo”, “age”: “14”, “company”: “aero”, “shift”: “night” }; Object.keys(obj2).forEach(function(key) … Read more

Vue 3 – “Failed to resolve component” with global components

Registering components in the root component’s components option doesn’t make them global. Doing that just makes them available to the root component itself, not its children. To register components globally, use app.component in your top-level code: main.js import { createApp } from ‘vue’; import App from ‘./App.vue’; import MyGlobalComponent from ‘./components/MyGlobalComponent.vue’; const app = createApp(App); … Read more

ES5 Object.assign equivalent

In underscore.js you can use like, _.extend(firstObj, secondObj); In jQuery, you can use, $.extend({},firstObj,secondObj); In pure javascipt, you can merge n number of objects by using this function: function mergeObjects() { var resObj = {}; for(var i=0; i < arguments.length; i += 1) { var obj = arguments[i], keys = Object.keys(obj); for(var j=0; j < … Read more