admin管理员组文章数量:1025218
I read here that i can add a key:value
to an object
only if the value is not null otherwise the key:value
will not be there at all :
var product = {
coverTitle:document.getElementById("coverTitle").value.length>0 ? getElementById("coverTitle").value : undefined,
..
..
Will print :
coverTitle:undefined
I expect to not have this key inside at all.
I read it here : In Javascript, how to conditionally add a member to an object?
answer by Frédéric Hamidi
When uploading to a server I do not want to write this pair at all, like they are not there. What am I doing wrong ?
I read here that i can add a key:value
to an object
only if the value is not null otherwise the key:value
will not be there at all :
var product = {
coverTitle:document.getElementById("coverTitle").value.length>0 ? getElementById("coverTitle").value : undefined,
..
..
Will print :
coverTitle:undefined
I expect to not have this key inside at all.
I read it here : In Javascript, how to conditionally add a member to an object?
answer by Frédéric Hamidi
When uploading to a server I do not want to write this pair at all, like they are not there. What am I doing wrong ?
Share Improve this question edited Jun 23, 2019 at 4:35 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Jun 23, 2019 at 4:26 phamngphamng 311 silver badge6 bronze badges 1- 1 The link you have posted is with jQuery. It will not work that way without it. – Avin Kavish Commented Jun 23, 2019 at 4:32
2 Answers
Reset to default 3You need to add the property later - undefined
key/value pairs are still kept in the object.
let product = {};
const value = document.getElementById("coverTitle").value;
if (value) product.coverTitle = value;
Alternatively, use JSON
methods to remove undefined
key/value pairs - costly, but it works:
var product = {
coverTitle: document.getElementById("coverTitle").value.length > 0 ? document.getElementById("coverTitle").value : undefined
};
product = JSON.parse(JSON.stringify(product));
You can conditionally add the key later on, but you can't write it inside the object literal.
const product = { }
const value = document.getElementById("coverTitle").value
if (value)
product.coverTitle = value
// or
value && (product.coverTitle = value)
I read here that i can add a key:value
to an object
only if the value is not null otherwise the key:value
will not be there at all :
var product = {
coverTitle:document.getElementById("coverTitle").value.length>0 ? getElementById("coverTitle").value : undefined,
..
..
Will print :
coverTitle:undefined
I expect to not have this key inside at all.
I read it here : In Javascript, how to conditionally add a member to an object?
answer by Frédéric Hamidi
When uploading to a server I do not want to write this pair at all, like they are not there. What am I doing wrong ?
I read here that i can add a key:value
to an object
only if the value is not null otherwise the key:value
will not be there at all :
var product = {
coverTitle:document.getElementById("coverTitle").value.length>0 ? getElementById("coverTitle").value : undefined,
..
..
Will print :
coverTitle:undefined
I expect to not have this key inside at all.
I read it here : In Javascript, how to conditionally add a member to an object?
answer by Frédéric Hamidi
When uploading to a server I do not want to write this pair at all, like they are not there. What am I doing wrong ?
Share Improve this question edited Jun 23, 2019 at 4:35 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Jun 23, 2019 at 4:26 phamngphamng 311 silver badge6 bronze badges 1- 1 The link you have posted is with jQuery. It will not work that way without it. – Avin Kavish Commented Jun 23, 2019 at 4:32
2 Answers
Reset to default 3You need to add the property later - undefined
key/value pairs are still kept in the object.
let product = {};
const value = document.getElementById("coverTitle").value;
if (value) product.coverTitle = value;
Alternatively, use JSON
methods to remove undefined
key/value pairs - costly, but it works:
var product = {
coverTitle: document.getElementById("coverTitle").value.length > 0 ? document.getElementById("coverTitle").value : undefined
};
product = JSON.parse(JSON.stringify(product));
You can conditionally add the key later on, but you can't write it inside the object literal.
const product = { }
const value = document.getElementById("coverTitle").value
if (value)
product.coverTitle = value
// or
value && (product.coverTitle = value)
本文标签: javascriptAdd value to object only if not nullis not workingStack Overflow
版权声明:本文标题:javascript - Add value to object only if not null-is not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611299a2159020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论