How can I clone a JavaScript object except for one key?
There is a Destructuring assignment syntax in JavaScript that can be used
let obj = {a: 1, b: 2, c: 3, z:26};
let {b, ...rest} = obj;
// skips the "Unused variable" warning
let {b: _, ...rest} = obj;
// removes property based on the dynamic key
const dynamicKey = "b";
let {[dynamicKey]: _, ...rest} = obj;
Modern browsers already support it out of the box.
See: JavaScript operator: Destructuring assignment: Rest in objects
For old browser versions there is an option to use Babel to support destructuring assignment. It will be transpiled into:
"use strict";
function _objectWithoutProperties(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
var x = { a: 1, b: 2, c: 3, z: 26 };
var b = x.b;
var y = _objectWithoutProperties(x, ["b"]);
Typescript how to clone object except for one key
What you need is spread operator (check the "Object Spread and Rest" part). Typescript is a superset of JavaScript, both of them support this feature.
const {adminPasswords, ...state} = initialStateFromDB;
const initialState: InitialState = {
...state,
adminPasswords: JSON.parse(adminPasswords)
}
Copy all elements from one object except those with that share their key with an element in another object
Your last example is a good start. You can combine it with Object.keys
to achieve what you want:
let obj1 = { a: 1, b: 2, c: 3, d: 4}
let obj2 = { a: "foo", c: "bar"}
const obj3 = Object.assign({}, obj1);Object.keys(obj2).forEach(key => delete obj3[key]);
console.log(obj3);
exclude specific key/value pairs by key name when copy contents from one object to another
You could use Object destructuring so that it will assign created
, updated
values, and rest other properties assign in a single object.
As you are using VUE, so add the following snippet before all import in script
tag in x.vue file
. SEE FOR VUE
// eslint-disable-next-line no-unused-vars
Disallow Unused Variables (no-unused-vars)
If error still persist then change the ESLint
config in package.json
"rules": {
"no-unused-vars": "off"
}
const dataFromParent = {
itemid: "104",
itemname: "insurance",
itemtype: "expense",
unitofmeasure: "$/yr",
created: "2021-04-14 05:59:30.097",
updated: "2021-04-14 05:59:30.097",
};
const { created, updated, ...rest } = dataFromParent; // make a shallow copy to avoid changing original object values
const data = rest;
console.log(data);
Check all value of Object except one
Use destructuring to extract the date and the rest of the properties and calculate the sum
of the Object.values using Array.reduce :
const obj = { date: "2019-10-03", hello: 0, yo: 0, test: 0 };
const { date, ...rest } = obj;
const sum = Object.values(rest).reduce((sum, curr) => sum + curr, 0);const allZeros = sum === 0 ? true : false;
console.log(allZeros);
Copy all elements from one object except those with that share their key with an element in another object
Your last example is a good start. You can combine it with Object.keys
to achieve what you want:
let obj1 = { a: 1, b: 2, c: 3, d: 4}
let obj2 = { a: "foo", c: "bar"}
const obj3 = Object.assign({}, obj1);Object.keys(obj2).forEach(key => delete obj3[key]);
console.log(obj3);
Related Topics
Throwing Strings Instead of Errors
Internal/Modules/Cjs/Loader.Js:582 Throw Err
Js String.Split() Without Removing the Delimiters
Jquery Difference Between Change and Click Event of Checkbox
Pass Arguments with Page.Evaluate
Higher-Order Functions in JavaScript
Es6 Iterate Over Class Methods
How Do Pipes and Monads Work Together in JavaScript
Accessing an Object's Property from an Event Listener Call in JavaScript
Javascript: Uncaught Typeerror: Cannot Call Method 'Addeventlistener' of Null
Browser Event When Downloaded File Is Saved to Disk
Merge Two Arrays So That the Values Alternate
Canvas Todataurl() Returns Blank Image
Jqgrid: Change Background Color of Row Based on Row Cell Value by Column Name
Vuejs 2.0 Emit Event from Grand Child to His Grand Parent Component