How to merge irregular object
I wanna merge 2 objects.
One object is the default form.
let data = {
weight: '',
DOB: '',
gender: '',
}
The other is irregular like below that
let temp1 = {
weight: '19.2',
}
let temp2 = {
DOB: '1992-11',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
let temp4 = undefined;
If I merge data
and temp1
, the result should be
let data = {
weight: '19.2',
DOB: '',
gender: ''
}
If I merge data
and temp3
, the result should be
let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}
If I merge data
and temp4
, the result should be
let data = {
weight: '',
DOB: '',
gender: ''
}
Give me some advice to achieve it.
Thanks in advance.
javascript object
add a comment |
I wanna merge 2 objects.
One object is the default form.
let data = {
weight: '',
DOB: '',
gender: '',
}
The other is irregular like below that
let temp1 = {
weight: '19.2',
}
let temp2 = {
DOB: '1992-11',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
let temp4 = undefined;
If I merge data
and temp1
, the result should be
let data = {
weight: '19.2',
DOB: '',
gender: ''
}
If I merge data
and temp3
, the result should be
let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}
If I merge data
and temp4
, the result should be
let data = {
weight: '',
DOB: '',
gender: ''
}
Give me some advice to achieve it.
Thanks in advance.
javascript object
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday
add a comment |
I wanna merge 2 objects.
One object is the default form.
let data = {
weight: '',
DOB: '',
gender: '',
}
The other is irregular like below that
let temp1 = {
weight: '19.2',
}
let temp2 = {
DOB: '1992-11',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
let temp4 = undefined;
If I merge data
and temp1
, the result should be
let data = {
weight: '19.2',
DOB: '',
gender: ''
}
If I merge data
and temp3
, the result should be
let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}
If I merge data
and temp4
, the result should be
let data = {
weight: '',
DOB: '',
gender: ''
}
Give me some advice to achieve it.
Thanks in advance.
javascript object
I wanna merge 2 objects.
One object is the default form.
let data = {
weight: '',
DOB: '',
gender: '',
}
The other is irregular like below that
let temp1 = {
weight: '19.2',
}
let temp2 = {
DOB: '1992-11',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
let temp4 = undefined;
If I merge data
and temp1
, the result should be
let data = {
weight: '19.2',
DOB: '',
gender: ''
}
If I merge data
and temp3
, the result should be
let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}
If I merge data
and temp4
, the result should be
let data = {
weight: '',
DOB: '',
gender: ''
}
Give me some advice to achieve it.
Thanks in advance.
javascript object
javascript object
asked yesterday
zynkn
3,90621030
3,90621030
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday
add a comment |
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday
add a comment |
2 Answers
2
active
oldest
votes
You can use Object.assign()
:
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data = Object.assign(data, temp3);
or if you don't want the data object to change:
let data2 = Object.assign({}, data, temp3);
The spread operator can also be used, which basically is the same, just that the original data object doesn't change:
data = { ...data, ...temp3 };
It's exactly what I found, I have known thespread operator
but I didn't know exactly how to use. Thank you a lot
– zynkn
yesterday
add a comment |
with jQuery
you can use $.extend()
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
OR use javascript spread operator ...
as following
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
If the keys are same then the value from righmost object is used
add a comment |
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%2f53944631%2fhow-to-merge-irregular-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Object.assign()
:
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data = Object.assign(data, temp3);
or if you don't want the data object to change:
let data2 = Object.assign({}, data, temp3);
The spread operator can also be used, which basically is the same, just that the original data object doesn't change:
data = { ...data, ...temp3 };
It's exactly what I found, I have known thespread operator
but I didn't know exactly how to use. Thank you a lot
– zynkn
yesterday
add a comment |
You can use Object.assign()
:
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data = Object.assign(data, temp3);
or if you don't want the data object to change:
let data2 = Object.assign({}, data, temp3);
The spread operator can also be used, which basically is the same, just that the original data object doesn't change:
data = { ...data, ...temp3 };
It's exactly what I found, I have known thespread operator
but I didn't know exactly how to use. Thank you a lot
– zynkn
yesterday
add a comment |
You can use Object.assign()
:
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data = Object.assign(data, temp3);
or if you don't want the data object to change:
let data2 = Object.assign({}, data, temp3);
The spread operator can also be used, which basically is the same, just that the original data object doesn't change:
data = { ...data, ...temp3 };
You can use Object.assign()
:
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data = Object.assign(data, temp3);
or if you don't want the data object to change:
let data2 = Object.assign({}, data, temp3);
The spread operator can also be used, which basically is the same, just that the original data object doesn't change:
data = { ...data, ...temp3 };
edited yesterday
answered yesterday
PierreDuc
28.9k45376
28.9k45376
It's exactly what I found, I have known thespread operator
but I didn't know exactly how to use. Thank you a lot
– zynkn
yesterday
add a comment |
It's exactly what I found, I have known thespread operator
but I didn't know exactly how to use. Thank you a lot
– zynkn
yesterday
It's exactly what I found, I have known the
spread operator
but I didn't know exactly how to use. Thank you a lot– zynkn
yesterday
It's exactly what I found, I have known the
spread operator
but I didn't know exactly how to use. Thank you a lot– zynkn
yesterday
add a comment |
with jQuery
you can use $.extend()
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
OR use javascript spread operator ...
as following
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
If the keys are same then the value from righmost object is used
add a comment |
with jQuery
you can use $.extend()
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
OR use javascript spread operator ...
as following
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
If the keys are same then the value from righmost object is used
add a comment |
with jQuery
you can use $.extend()
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
OR use javascript spread operator ...
as following
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
If the keys are same then the value from righmost object is used
with jQuery
you can use $.extend()
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
OR use javascript spread operator ...
as following
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
If the keys are same then the value from righmost object is used
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}
console.log($.extend(data,temp1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
let data = {
weight: '',
DOB: '',
gender: '',
}
let temp3 = {
DOB: '1992-11',
gender: 'Male',
}
data={...data,...temp3}
console.log(data)
edited yesterday
answered yesterday
Elish
1718
1718
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53944631%2fhow-to-merge-irregular-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
Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday
If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday
@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday