无法将 JSON 数组与 JavaScript 数组进行比较


Can't Compare JSON Array with JavaScript Array

好的,所以我有一个JSON对象,我将其作为参数发送到test()函数。见下文:

<script type="text/javascript">
$('#getdata').click(function(){

    $.ajax({                                      
    url: '<?php echo base_url().'jqueryDB/jquery';?>',                  //the script to call to get data    
    type:'POST',      
    data: "",                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
    dataType: 'json',                //data format      
    success: function(output_string){
                    $("#result_table").append(output_string);
                    test(output_string);
    }
  });
}); 
</script>

我的测试函数如下所示:(注释部分解释了更多

<script id="template-download" type="text/x-tmpl">
    window.output = [];  // global array
    function test(arg){
    output = arg;
    {% for (var i=0, file; file=o.files[i]; i++) { %}
        <tr class="template-download fade">
            {% if (file.error) { %}
                <td></td>
                <td class="name"><span>{%=file.name%}</span></td>
                <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
                <td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
      // This is where I am having problems. I specify index zero for output and sooner or
     // later file.name should match, but it never does and I'm not sure why??
    // So then I do else if ( "5.jpg" == file.name ) and it does work....As you can
   // see that I test it in the line right below the else if, and it displays the same as file.name.
            {% } else if ( output[0].localeCompare( file.name ) ) { %}
                    <td class="name">{%=String(output[0])%}{%=String(file.name)%}</td>
                    <td class="preview">{% if (file.thumbnail_url) { %}
                        <a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
                    {% } %}</td>
                    <td class="name">
                        <a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
                    </td>
                    <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
                    <td colspan="2"></td>
                {% } %}
                    <td class="delete">
                        <button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
                        <i class="icon-trash icon-white"></i>
                        <span>{%=locale.fileupload.destroy%}</span>
                        </button>
                        <input type="checkbox" name="delete" value="1">
                    </td>
        </tr>
        }
    {% } %}
}

谁能看到我在比较中哪里出了问题?当我这样做时else if ( output[0].localeCompare( "5.jpg" ) ) { %}它也不能正常工作。因此,即使我测试时屏幕上的输出显示 5.jpg,JSON 对象也不允许我将其与自身以外的其他内容进行比较。我确实将它与它本身进行了比较,当然它;)有效。

这些是不同的陈述:

if ("5.jpg" == file.name)
if ("5.jpg".localeCompare(file.name))

如果两个字符串相同,则第一次比较将返回true并将运行 if 语句的正文。 如果两个字符串相同,localeCompare将返回零,这在if语句的上下文中与false相同。 你可能想要

{% } else if (output[0].localeCompare(file.name) == 0) { %}