最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Add value to object only if not null-is not working - Stack Overflow

matteradmin7PV0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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)
Post a comment

comment list (0)

  1. No comments so far