So let's say that I have an object literal, which is the following:
let test = {
settings: {
title: '',
has_content: true,
can_edit: false,
}
}
Now I have another object literal as shown below:
let test2 = {
...test,
settings: {
can_edit: true,
}
}
How can I bring over all the parameters from the test object literal but have the ability to change values in test2?
So ultimately, I'd like for this:
let test2 = {
settings: {
title: '', (This is default)
can_edit: true, (This value is changed)
has_content: true, (This is default)
}
}
Now, when I console.log test2, all that I see in the settings is the can_edit: true, but not the title or has_content.
You need to spread the test.settings in test2.settings to merge the properties and override with the latest one that are defined in test2.settings.
let test = {
settings: {
title: "",
has_content: true,
can_edit: false,
},
};
let test2 = {
settings: {
...test.settings,
can_edit: true,
},
};
console.log(test2);
Because you test.settings key will be replaced by test2.settings. so you should spread the test.setting into test2.settings like below
let test = {
settings: {
title: '',
has_content: true,
can_edit: false,
}
}
let test2 = {
settings: {
...test.settings,
can_edit: true,
}
}
console.log(test2)
It should be like this
let test2 = {
...test,
settings: {
...test.settings,
can_edit: true,
}
}
Explanation: You need to spread object by object. Be aware, if you use the spread before changing the values (like I did) your changes survive.
Related
This is my current object which i get but i want to filter out only true values and also maintain the same structure in return.
Current Object =
errors :{
frontdesk: {
PreAudit: true,
AuthSent: false,
Limitation: false,
},
clinical: {
medicaid: true,
Vitals: true,
Height: false,
},
eligibilityVerification: {
Mentioned: true,
EVAttached: false,
}
}
i want like this =
errors :{
frontdesk: {
PreAudit: true,
},
clinical: {
medicaid: true,
Vitals: true,
},
eligibilityVerification: {
Mentioned: true,
}
}
To filter out only the true values in the object, you can use the Object.entries()
const filteredErrors = Object.entries(errors)
.filter(([key, value]) => value)
.reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});
This will return a new object with the same structure as the original errors object, but with only the true values.
I'm trying to use the ng-multiselect-dropdown https://www.npmjs.com/package/ng-multiselect-dropdown. I've got an array, that contains nested objects. The structure looks some kind like this:
obj1: {
id: string;
atr1: {
atr2: string;
atr3: string;
}
}
My question:
How can I display for example atr2 in the dropdown-menu? This is my dropdown-setting:
dropdownSettingsProjectManagers: any = {
singleSelection: false,
idField: 'id',
textField: 'atr1.atr2', //? What do I have to put in here?
showSelectedItemsAtTop: true,
clearSearchFilter: true,
allowSearchFilter: true,
enableCheckAll: false,
allowRemoteDataSearch: true
};
Thanks, for every help.
trying to figure out how to dynamicly create a new nested object from this one:
object1 = {
DataStore : false,
Header: false,
Footer : false,
Sidebar : false,
Main : false,
}
to nested one like this:
const registerComponentsLocal = {
'DataStore': {
'debug': false
},
'Header': {
'debug': false
},
'Footer': {
'debug': false
},
'Sidebar': {
'debug': false
},
'Main': {
'debug': false
},
}
keys and values have to by dynamic. Only important thing is a structure of the final object.
Any ideas would be greatly appricieated.
To create a new instance (i.e preserve the old one)
let originalObject = {
DataStore : false,
Header: false,
Footer : false,
Sidebar : false,
Main : false,
}
let newObject = Object.assign({}, originalObject) // Copies the original object
Object.entries(newObject).forEach(([key, value]) => newObject[key] = {debug: value})
Here's a method using reduce
Object.entries(object1)
.reduce((b,a) => ({...b, [a[0]] : {debug:a[1]}}), {})
To iterate, we need an array and Object.entries gives us that. Then, using reduce, we iterate through each item in object1, and build a result. Here, this line ({...b, [a[0]] : {debug:a[1]}}) takes our accumulating object b and adds in the next iterable: {key: { debug: value}}`
let object1 = {
DataStore : false,
Header: false,
Footer : false,
Sidebar : false,
Main : false,
}
const registerComponentsLocal = Object.entries(object1).reduce((b,a) => ( {...b, [a[0]] : { debug:a[1]} }),{})
console.log(registerComponentsLocal)
I know everyone may had this issue before and I think this one maybe the same but what I expected is the 'uniq_identifier' should be series numbers, like 0, 1, 2, 3.... but I always get 1 for ever.
enter code here
array.forEach(element => {
let categoryId = 'categoryId'
let body = 'Press and hold for more options, God bless USA!';
console.log(`completeID-${element}`);
var uniq_identifier = `completeID-${element}`;
Notifications.createCategoryAsync(uniq_identifier, [
{
actionId: uniq_identifier,
buttonTitle: `USA ${element}`,
isDestructive: false,
isAuthenticationRequired: false,
},
{
actionId: 'God and me',
buttonTitle: 'Delay',
isDestructive: false,
isAuthenticationRequired: false,
},
]).then(() => {
console.log('var in async: ', element);
});
});
Using this code I have this issue:
$.fn.dxwShow = function (options)
{
console.log(typeof(options));
dxwShowSetOptions(options);
setInterval(function(){
dxwShowChange();
}, dxwShowOptions.time);
};
var dxwShowOptions = {
"transition" : "SlideToggle",
"time": 1000
};
var dxwShowStatus = {
current : 0
};
function dxwShowSetOptions(options)
{
console.dir(typeof(options));
dxwShowOptions = Object.create(dxwShowOptions, options);
}
function dxwShowChange()
{
console.log(dxwShowOptions);
};
$(function()
{
options = {
"time": 700,
"debug" : true
};
$("#dxwShow").dxwShow(options);
});
I want to update dxwShowOptions and so I use Object.create passing first the object I wanna copy and so the object containing the new parameters. Where is the mistake?
PS :Chrome say that the object is at the Object.create line.
Object.create takes a map of property descriptors. options is not such a list.
See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
If you wanted to still use Object.create, you'd need to modify options to be something more like
var options = {
time: {
enumerable: true,
configurable: true,
writable: true,
value: 700
},
debug: {
enumerable: true,
configurable: true,
writable: true,
value: true
}
};
But probably you want to use something more like _.extend.