score:-2
const A = { activity: 'purchased', count: undefined, time: '09:05:33' }
const B = { activity: 'purchased', count: '51', time: undefined }
const AKeys = Object.keys(A);
const BKeys = Object.keys(B);
const C = {};
AKeys.forEach(element=>A[element] && C[element]=A[element])
BKeys.forEach(element=>B[element] && C[element]=B[element])
score:-2
let A = { activity: 'purchased', count: undefined, time: '09:05:33' }
let B = { activity: 'purchased', count: '51', time: undefined }
for (let a in A) {
if (A[a] === undefined)
delete A[a];
}
for (let b in B) {
if (B[b] === undefined)
delete B[b];
}
let c = {...A, ...B} // this will merge identical key/values
console.log(c)
score:0
You could merge the objects by having a look to the entries of the second object and take only key/value pairs without undefined
as value.
const
merge = (a, b) => Object.assign(
{},
a,
...Object.entries(b).map(([k, v]) => v === undefined ? {} : { [k]: v })
),
a = { activity: 'purchased', count: undefined, time: '09:05:33' },
b = { activity: 'purchased', count: '51', time: undefined };
console.log(merge(a, b));
score:1
You could use lodash's merge command. C = _.merge(A,B);
score:2
let A = { activity: 'purchased', count: undefined, time: '09:05:33' }
let B = { activity: 'purchased', count: '51', time: undefined }
let C={}
Object.keys({...A,...B}).map(key=>{
C[key]=B[key]||A[key]
})
console.log(C)
score:3
The spread operator(...) works well to merge objects, and there is a simple solution to remove undefined using JSON.stringify() and JSON.parse(). See below example:
const A = { activity: 'purchased', count: undefined, time: '09:05:33' };
const B = { activity: 'purchased', count: '51', time: undefined };
//If you don't care about date objects then only use below method
const C = {...JSON.parse(JSON.stringify(A)), ...JSON.parse(JSON.stringify(B))};
console.log(C);
Source: stackoverflow.com
Related Query
- Merge two objects in javascript ignoring undefined values
- Of two equal JSON objects, accessing values in javascript works in one case, the other returns undefined
- How do I merge two Javascript Objects into one?
- Comparing two arrays of nested objects and return new array of objects if compared values are not the same in javascript
- How to convert float values in JSON to two decimal place using Javascript
- How to merge two array of objects with reactjs?
- Adding up the values of an array of objects javascript
- How can you merge two objects into same property in JavaScript?
- ReactJS : Immutable.js : merge two Json objects
- Merge array of javascript objects
- Merge two arrays into one Array of objects React
- Objects with data into object with list of keys and values - javascript
- Merge properties of objects within a array together using values and remove duplicate
- JavaScript (ReactJS) comparing two Objects
- How to merge an object with other object inner values in JavaScript without going through a loop?
- Looping over an array of objects and showing values based on static keys defined javascript
- Javascript how to see if two objects with array of string contain the same value
- Re-initializing react state array of objects leads to undefined values
- Javascript filter Array of objects with multiple conditions and values
- How to compare objects of two different arrays in javascript
- How do I combine two objects by merging the values of same properties?
- How to process JSON Array of objects using Key values in JavaScript
- Finding values or objects in a json file with nested objects and passing the result object to child in Javascript and react
- Filtering (searching) for two values (search criteria) in Javascript
- trying to merge two objects and return merged object in react
- javascript / react - switching the properties of two objects in an array in react state without triggering a re-render
- Merge two arrays of different objects into one array of objects combined from the first two
- Merge objects from the same array by two with different keys
- Javascript -> Adding up values of nested objects with the same name
- Javascript merge the values of 4 arrays into one object
More Query from same tag
- React prevState changing when I change its clone, cloned with newArray = [...prevState.oldArray]
- How to make select all checkbox always visible in office-ui-fabric-react DetailsList
- React nested route fails to load on refresh
- setState works but redux store update doesn't
- How to wrap ag-grid-react into component?
- React Day Picker: How to disable interaction with disabled dates in "Select Multiple dates"
- React component. Render specific element
- Hide/Show a component when clicking on the X
- Using Immutable js, is there a better way to update the value of this array in an immutable object?
- How to configure react-hooks/exhaustive-deps additionalHooks for modules?
- How to add value to the children in nested array of objects
- React state mapping acting very strangely?
- setState inside a useEffect not working in enzyme
- setState in react js, with ES6 destructuring
- Opacity filter makes text less white, how can I fix it?
- OnClick function not working with custom button Component
- How to disable a button once clicked in JSX, ReactJs?
- React-Redux: Value passing from one page to another
- How to stop drawing after completing one circle in DrawingManager on react-google-maps?
- global.location not working in Jest tests
- Laravel Sanctum returning 401 when trying to get the authenticated user
- Show a component box on hover like dropdown menu and popup
- Is there any way to stop on hydration React App in Cypress?
- Convert blob to pdf file
- nextjs - shopping cart problem with accurate cart total amount
- How to pass data to another file?
- JavaScript index.js .babelrc error "Do not clone comments tha...<omitted>... } could not be cloned"
- On React hook component unmount, unable to get updated state variable value
- Uncaught TypeError: Cannot read property 'touched' of undefined
- Toggle isActive className on div element inside map function