0

I don't know if I'm clear, but what I'm trying to do is something like this:

 ajaxUpdate= $.ajax({
                        url: "updateSheet.php",
                        type: "post",
                        data: {
                            'update': 1,
                            'id': $('#numbah'+numbah+'').attr("class"),
                            'x': if (x) x,
                            'y': if (y) y,
                            'title': if (title) title,
                            'content': if (content) content
                        }
                    });

is something like 'x': if (x) x, correct? I can't risk running this part of code corrupted, because it can mess with my file.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Kajcioch
  • 175
  • 1
  • 8
  • No, as you'll notice if you open your developer console, that syntax is not correct. In JS, `if` is not an expression. JS does have a [conditional operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) though, that'll let you do `x ? x : ""`. Or you can use a [logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) `x || ""`. In both cases a default value of `""` is provided. There's no way to elide the entire property inline. –  May 11 '16 at 18:37
  • 1
    Your code / problem has nothing to do with JSON. It seems you are confusing an object literal with JSON. – Felix Kling May 11 '16 at 18:47
  • Probably a duplicate of [How to write an inline IF statement in JavaScript?](http://stackoverflow.com/q/10270351/218196) – Felix Kling May 11 '16 at 18:49

3 Answers3

2

Just use the ternary operator and do x ? x : ''. This says if x is truthy (in your case if it's set) then return x otherwise return the empty string. So you could do something like this:

ajaxUpdate= $.ajax({
                    url: "updateSheet.php",
                    type: "post",
                    data: {
                        'update': 1,
                        'id': $('#numbah'+numbah+'').attr("class"),
                        'x': x ? x : '',
                        'y': y ? y : '',
                        'title': title ? title : '',
                        'content': content ? content : ''
                    }
                });
winhowes
  • 7,845
  • 5
  • 28
  • 39
1

Javascript has a three-way-operator ?: which you could use like so:

title: title ? title : '',
content: content ? content : ''

This sends an empty string, if the variable is empty or the content of it if not.

Alternatively, you can use the logical OR operator ||:

title: title || ''
content: content || ''

It's a little shorter.

JSchirrmacher
  • 3,243
  • 2
  • 19
  • 27
1

You can use ternary operator - info MDN

ajaxUpdate= $.ajax({
                    url: "updateSheet.php",
                    type: "post",
                    data: {
                        'update': 1,
                        'id': $('#numbah'+numbah+'').attr("class"),
                        'x': x == someValue ? x: '',
                        'y': y == someValue ? y: '',
                        'title': if (title) title,
                        'content': if (content) content
                    }
                });
mihkov
  • 1,171
  • 13
  • 37