Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 相同的数组会产生不同的结果_Javascript_Arrays_Ajax_Gist - Fatal编程技术网

Javascript 相同的数组会产生不同的结果

Javascript 相同的数组会产生不同的结果,javascript,arrays,ajax,gist,Javascript,Arrays,Ajax,Gist,脚本标记中最接近的函数查找最接近112的数字。但是,当我在来自ajax请求的相同数组上使用相同的函数时,它给出了错误的结果 控制台日志1:本地数组生成正确的结果122 控制台日志2:显示的外部阵列2与本地阵列相同 控制台日志3:使用外部阵列会导致左括号中出现错误。从gist中删除括号将得到8,这仍然是不正确的 函数loadgistgistid,文件名 { $.ajax{ 网址:'https://api.github.com/gists/“+gistid, 键入:“GET”, 数据类型:“jso

脚本标记中最接近的函数查找最接近112的数字。但是,当我在来自ajax请求的相同数组上使用相同的函数时,它给出了错误的结果

控制台日志1:本地数组生成正确的结果122

控制台日志2:显示的外部阵列2与本地阵列相同

控制台日志3:使用外部阵列会导致左括号中出现错误。从gist中删除括号将得到8,这仍然是不正确的

函数loadgistgistid,文件名 { $.ajax{ 网址:'https://api.github.com/gists/“+gistid, 键入:“GET”, 数据类型:“jsonp” }.successfunctiongistdata{ var content=gistdata.data.files[filename].content; 有内容物的物质 }错误函数{ //ajax错误 }; } 函数DoSomethingWithcontent { 数字=112; var array2=含量; console.logcontent; console.logclosestnumber,array2; } 加载GIST9544DFD755418E819810312488E7986C,gistfile1.txt; 函数closestnum,arr { var curr=arr[0]; var diff=数学.abs num-curr; 对于var val=0;val
function DoSomethingWith(content)
{
    let number = 112;
    let array2 = JSON.parse(content);
    console.log(content);
    console.log(closest(number, array2));
}
如果将console.logtypeof内容添加到DoSomethingWith方法中。。。您将注意到内容不是数组,而是字符串。因此,使用从字符串中获取数组,如下所示:

function DoSomethingWith(content)
{
    let number = 112;
    let array2 = JSON.parse(content);
    console.log(content);
    console.log(closest(number, array2));
}

函数最接近的num,arr{console.logtypeof arr下拉菜单使用来自Ajax请求的数组构建。函数最接近的num,arr{console.logtypeof arr下拉菜单是使用Ajax请求中的数组构建的。如果您有时间,您会推荐什么其他表达式,它们与调试目的的“type of”一样有用。这只是一个观察结果,但在浏览器的开发工具中,第一个控制台日志使用颜色计算条目,其他条目为黑色。是否要评估字符串评价为黑色?非常感谢Shidersz。@DerekM抱歉,但对于调试,我现在没有想到其他人。关于在开发人员控制台上显示数据的方式,我认为这可能取决于浏览器。就个人而言,我不认为这有什么普遍性。如果你有时间,你会重新考虑其他的表达方式吗mmend,这与调试目的的“type of”一样有用。只是一个观察,但在浏览器的开发工具中,第一个控制台日志使用颜色计算条目,其他条目为黑色。字符串是否计算为黑色?非常感谢Shidersz。@DerekM抱歉,但对于调试,我现在想不出其他条目了。@n关于在开发者控制台上显示数据的风格,我认为这可能取决于浏览器。就我个人而言,我不认为这有什么普遍性。