如果未定义,那么" 0"在appendTo

时间:2015-12-25 12:53:23

标签: javascript jquery

我正在寻找一种有效的方法来检查值是否未定义,然后将其转换为" 0"。

代码示例是

$('<tr><td>' + el.firstStat  +'</td><td>'+ el.hours +'</td></tr>').appendTo('#table tbody'); 

这些值中的任何一个都可以是未定义的,并且将来可能会有更多值要添加。

4 个答案:

答案 0 :(得分:5)

只需使用el.firstStat = el.firstStat || 0; el.hours = el.hours || 0; $('<tr><td>' + el.firstStat +'</td><td>'+ el.hours +'</td></tr>').appendTo('#table tbody'); (OR)运算符:

undefined || 0

因为0 override func viewDidAppear(animated: Bool) { if CLLocationManager.locationServicesEnabled(){ switch CLLocationManager.authorizationStatus() { case .NotDetermined, .Restricted, .Denied: print("No access") case .AuthorizedAlways, .AuthorizedWhenInUse: let geocoder = CLGeocoder() longitude = self.locationManager.location!.coordinate.longitude latitude = self.locationManager.location!.coordinate.latitude geocoder.reverseGeocodeLocation(CLLocation(latitude: (latitude), longitude: (longitude)), completionHandler: {placemarks, error in if error == nil && placemarks!.count > 0 { self.thoroughfare = (placemarks!.last?.thoroughfare)! self.city = (placemarks!.last?.locality)! print(self.thoroughfare) print(self.city) print(self.longitude) print(self.latitude) } }) } } else { print("Location services are not enabled") } }

答案 1 :(得分:2)

如果这些值始终是数值,则可以使用速记类型转换($('<tr><td>' + (0 + el.firstStat) +'</td><td>'+ (0 + el.hours) +'</td></tr>').appendTo('#table tbody'); )来获取这些零:

$('<tr><td>' + (el.firstStat || 0)  +'</td><td>'+ (el.hours || 0) +'</td></tr>').appendTo('#table tbody');

或使用逻辑OR:

using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Diagnostics;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var compiler = new CSharpCodeProvider();
            var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
            parameters.CompilerOptions = "/platform:x64";
            parameters.GenerateExecutable = true;

            CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
                                    @"using System;
                                     class Program {
                                           public static void Main(string[] args) {
                                           Console.WriteLine(""Hello world!"");
                                           Console.ReadLine();
                                           }
                                      }");

            var testProcess = new Process();
            testProcess.StartInfo.FileName = results.CompiledAssembly.CodeBase;
            testProcess.Start();

            Console.WriteLine("I've run slave!");

            Console.ReadLine();
        }
    }
}

答案 2 :(得分:0)

$('<tr><td>' + (el.firstStat==undefined)?"0":el.firstStat  +'</td><td>'+ (el.hours==undefined)?"0":el.hours +'</td></tr>').appendTo('#table tbody'); 

试试这个

答案 3 :(得分:0)

您可以使用三元条件:

 el.firstStat = el.firstStat === undefined ? 0 : el.firstStat;

或更短,双管符号:

 el.firstStat = el.firstStat || 0;