Updating property values dynamically is replacing the entire object
I'm trying to update a dataStore service I'm building and I'm hung up on the strangest problem.
public setOptions(params: Params): void {
console.log(this._dataStore.options); // always empty here!!!
for (const key of Object.keys(params)) {
this._dataStore['options'][key] = params[key];
}
console.log(this._dataStore.options); // parameter(s) added
}
Calling setOptions()
with the initial set of URL parameters works and _dataStore.options
contain the properties reflecting the incoming parameters.
The problem is anytime I call setOptions the options properties are replaced by the new params
instead of updating them or adding new additions.
console.log(this._dataStore.options); // city: la
This makes no sense to me, can someone help me understand and how to properly update the .options object?
typescript object parameters
|
show 1 more comment
I'm trying to update a dataStore service I'm building and I'm hung up on the strangest problem.
public setOptions(params: Params): void {
console.log(this._dataStore.options); // always empty here!!!
for (const key of Object.keys(params)) {
this._dataStore['options'][key] = params[key];
}
console.log(this._dataStore.options); // parameter(s) added
}
Calling setOptions()
with the initial set of URL parameters works and _dataStore.options
contain the properties reflecting the incoming parameters.
The problem is anytime I call setOptions the options properties are replaced by the new params
instead of updating them or adding new additions.
console.log(this._dataStore.options); // city: la
This makes no sense to me, can someone help me understand and how to properly update the .options object?
typescript object parameters
I'm not sure what you expect to happen. Can you explain further with some examples of_dataStore.options
both before and after callingsetOptions()
multiple times?
– Phil
Nov 22 '18 at 23:05
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Yes I am trying to merge them. I explain in the codeproperties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Can you please show examples of what you expect to happen given specificparams
– Phil
Nov 22 '18 at 23:11
There is absolutely nothing in the code shown that would remove theclaimed
andlocation
properties from_dataStore.options
– Phil
Nov 22 '18 at 23:13
|
show 1 more comment
I'm trying to update a dataStore service I'm building and I'm hung up on the strangest problem.
public setOptions(params: Params): void {
console.log(this._dataStore.options); // always empty here!!!
for (const key of Object.keys(params)) {
this._dataStore['options'][key] = params[key];
}
console.log(this._dataStore.options); // parameter(s) added
}
Calling setOptions()
with the initial set of URL parameters works and _dataStore.options
contain the properties reflecting the incoming parameters.
The problem is anytime I call setOptions the options properties are replaced by the new params
instead of updating them or adding new additions.
console.log(this._dataStore.options); // city: la
This makes no sense to me, can someone help me understand and how to properly update the .options object?
typescript object parameters
I'm trying to update a dataStore service I'm building and I'm hung up on the strangest problem.
public setOptions(params: Params): void {
console.log(this._dataStore.options); // always empty here!!!
for (const key of Object.keys(params)) {
this._dataStore['options'][key] = params[key];
}
console.log(this._dataStore.options); // parameter(s) added
}
Calling setOptions()
with the initial set of URL parameters works and _dataStore.options
contain the properties reflecting the incoming parameters.
The problem is anytime I call setOptions the options properties are replaced by the new params
instead of updating them or adding new additions.
console.log(this._dataStore.options); // city: la
This makes no sense to me, can someone help me understand and how to properly update the .options object?
typescript object parameters
typescript object parameters
edited Nov 23 '18 at 15:05
TR3B
asked Nov 22 '18 at 23:00
TR3BTR3B
1,60422860
1,60422860
I'm not sure what you expect to happen. Can you explain further with some examples of_dataStore.options
both before and after callingsetOptions()
multiple times?
– Phil
Nov 22 '18 at 23:05
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Yes I am trying to merge them. I explain in the codeproperties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Can you please show examples of what you expect to happen given specificparams
– Phil
Nov 22 '18 at 23:11
There is absolutely nothing in the code shown that would remove theclaimed
andlocation
properties from_dataStore.options
– Phil
Nov 22 '18 at 23:13
|
show 1 more comment
I'm not sure what you expect to happen. Can you explain further with some examples of_dataStore.options
both before and after callingsetOptions()
multiple times?
– Phil
Nov 22 '18 at 23:05
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Yes I am trying to merge them. I explain in the codeproperties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Can you please show examples of what you expect to happen given specificparams
– Phil
Nov 22 '18 at 23:11
There is absolutely nothing in the code shown that would remove theclaimed
andlocation
properties from_dataStore.options
– Phil
Nov 22 '18 at 23:13
I'm not sure what you expect to happen. Can you explain further with some examples of
_dataStore.options
both before and after calling setOptions()
multiple times?– Phil
Nov 22 '18 at 23:05
I'm not sure what you expect to happen. Can you explain further with some examples of
_dataStore.options
both before and after calling setOptions()
multiple times?– Phil
Nov 22 '18 at 23:05
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Yes I am trying to merge them. I explain in the code
properties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Yes I am trying to merge them. I explain in the code
properties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Can you please show examples of what you expect to happen given specific
params
– Phil
Nov 22 '18 at 23:11
Can you please show examples of what you expect to happen given specific
params
– Phil
Nov 22 '18 at 23:11
There is absolutely nothing in the code shown that would remove the
claimed
and location
properties from _dataStore.options
– Phil
Nov 22 '18 at 23:13
There is absolutely nothing in the code shown that would remove the
claimed
and location
properties from _dataStore.options
– Phil
Nov 22 '18 at 23:13
|
show 1 more comment
1 Answer
1
active
oldest
votes
Merge them with Object.assign()
for (const key of Object.keys(params)) {
Object.assign(this._dataStore['options'][key] , params[key]);
// or reverse arguments if applicable
}
Hey Chalietfl this is where I began only I thoughtObject.assign
was meant to combine properties into a new object? Also it's giving me an errorTypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438886%2fupdating-property-values-dynamically-is-replacing-the-entire-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Merge them with Object.assign()
for (const key of Object.keys(params)) {
Object.assign(this._dataStore['options'][key] , params[key]);
// or reverse arguments if applicable
}
Hey Chalietfl this is where I began only I thoughtObject.assign
was meant to combine properties into a new object? Also it's giving me an errorTypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
|
show 4 more comments
Merge them with Object.assign()
for (const key of Object.keys(params)) {
Object.assign(this._dataStore['options'][key] , params[key]);
// or reverse arguments if applicable
}
Hey Chalietfl this is where I began only I thoughtObject.assign
was meant to combine properties into a new object? Also it's giving me an errorTypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
|
show 4 more comments
Merge them with Object.assign()
for (const key of Object.keys(params)) {
Object.assign(this._dataStore['options'][key] , params[key]);
// or reverse arguments if applicable
}
Merge them with Object.assign()
for (const key of Object.keys(params)) {
Object.assign(this._dataStore['options'][key] , params[key]);
// or reverse arguments if applicable
}
edited Nov 22 '18 at 23:18
answered Nov 22 '18 at 23:11
charlietflcharlietfl
140k1389123
140k1389123
Hey Chalietfl this is where I began only I thoughtObject.assign
was meant to combine properties into a new object? Also it's giving me an errorTypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
|
show 4 more comments
Hey Chalietfl this is where I began only I thoughtObject.assign
was meant to combine properties into a new object? Also it's giving me an errorTypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
Hey Chalietfl this is where I began only I thought
Object.assign
was meant to combine properties into a new object? Also it's giving me an error TypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Hey Chalietfl this is where I began only I thought
Object.assign
was meant to combine properties into a new object? Also it's giving me an error TypeError: Cannot convert undefined or null to object
– TR3B
Nov 22 '18 at 23:19
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
Target can be a new object or if it is existing one it copies properties and values from source(s)
– charlietfl
Nov 22 '18 at 23:20
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
If you have that error likely that dataStore doesn't have that key and need to create an object in that instance. If you would just provide some sample data in order to run this it would help. Hard for us to know how either object is constructed
– charlietfl
Nov 22 '18 at 23:22
Hmm dataStore is instantiated like this:
private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Hmm dataStore is instantiated like this:
private _dataStore = { options: {}, results: };
– TR3B
Nov 22 '18 at 23:23
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
Ok then it would seem you need to check if keys exist first
– charlietfl
Nov 22 '18 at 23:24
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438886%2fupdating-property-values-dynamically-is-replacing-the-entire-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not sure what you expect to happen. Can you explain further with some examples of
_dataStore.options
both before and after callingsetOptions()
multiple times?– Phil
Nov 22 '18 at 23:05
Are you trying to merge them with existing perhaps? Issue is not clear at all
– charlietfl
Nov 22 '18 at 23:08
Yes I am trying to merge them. I explain in the code
properties are being replaced, not updated or added
– TR3B
Nov 22 '18 at 23:10
Can you please show examples of what you expect to happen given specific
params
– Phil
Nov 22 '18 at 23:11
There is absolutely nothing in the code shown that would remove the
claimed
andlocation
properties from_dataStore.options
– Phil
Nov 22 '18 at 23:13