简体   繁体   English

有人可以解释以下javascript代码段吗? (菜鸟)

[英]Can someone please explain the following javascript code snippets? (NOOB)

The code is part of a program which returns the largest number within an array. 该代码是程序的一部分,该程序返回数组中的最大数字。 Could someone please explain the following? 有人可以解释以下内容吗? Thanks 谢谢

  if (parseInt(nums[0]) < parseInt(nums[1])) { nums.splice(0,1); }
        else { nums.splice(1,1); }

The basic is understanding what .splice does MDN 基本的是了解.spliceMDN

So it will modify your array by removing an item depending on the passed params 因此,它将根据传递的参数通过删除项目来修改数组

array . 数组 splice (start[, deleteCount[, item1[, item2[, ...]]]]) 拼接 (start [,deleteCount [,item1 [,item2 [,...]]]])

 var nums = ["2", 4]; if (parseInt(nums[0]) < parseInt(nums[1])) { nums.splice(0,1); // At index 0 remove 1 item } else { nums.splice(1,1); // At index 1 remove 1 item } console.log( nums ) // Modified array 

PS: PS:

  • as you noticed, the code works only for Arrays of length 2; 如您所见,该代码仅适用于长度为2的数组; it only compares the 0 and 1 index: nums[0] nums[1] 它仅比较0和1索引: nums[0] nums[1]
  • If your array is [1, 1] (same values) - nothing will happen. 如果您的数组是[1, 1] (相同的值),则不会发生任何事情。
  • If your array is [1.9, 1] - [1] will be returned, since you're using parseInt 如果您的数组为[1.9, 1] -将返回[1] ,因为您正在使用parseInt
  • It's good habit to use radix with parseInt(value, radix) like parseInt(nums[0], 10) 基数parseInt(nums[0], 10)一起使用parseInt(value, radix)是一个好习惯

Since you wrote NOOB I think it's important to be clear. 自从您写了NOOB以来,我认为重要的是要弄清楚。

The splice method deletes 0 or more items from an array at a specified index and inserts 0 or more items at that index. splice方法从指定索引处的数组中删除0个或多个项目,并在该索引处插入0个或多个项目。

The signature is someArray.splice(startNdx, deleteCount[, ...itemsToInsert]) 签名是someArray.splice(startNdx, deleteCount[, ...itemsToInsert])

In the code you posted, this part 在您发布的代码中,这部分

nums.splice(0,1);

starts at index 0 and deletes 1 element from the array. 从索引0开始,并从数组中删除1个元素。 In other words if the array was [5, 6, 7] it would become [6, 7] 换句话说,如果数组为[5, 6, 7] ,它将变为[6, 7]

nums.splice(1,1);

This part starts at index 1 and deletes 1 element from the array. 这部分从索引1开始,并从数组中删除1个元素。 In other words if the array was [5, 6, 7] it would become [5, 7] . 换句话说,如果数组为[5, 6, 7] ,它将变为[5, 7]

The parseInt parts convert strings to integers which suggests the array contains strings not numbers (or might optionally contain strings). parseInt部分将字符串转换为整数,这表明数组包含的字符串不是数字(或者可以选择包含字符串)。 Assuming the array does contain strings then without the parseInt part it would be comparing strings not numbers which is a different type of compare. 假设数组确实包含字符串,那么如果没有parseInt部分,它将比较字符串而不是数字,这是一种不同的比较类型。 Since you didn't show the contents of the array it's hard to know exactly what it's trying to do but for example 由于您没有显示数组的内容,因此很难确切知道它要做什么。

"00" < "0000"  

is true where as 是真的

parseInt("00") < parseInt("0000") 

is not true. 是不正确的。 Similarly 相似地

"000_bob" < "000_jill" is true

but

parseInt("000_bob") < parseInt("000_jill") 

is not true 不是真的

So the code is checking if the first value is less than the second but it's comparing by integer instead of string. 因此,代码正在检查第一个值是否小于第二个值,但它是按整数而不是字符串进行比较。

Someone else claimed this was bad code but it's not bad code without knowing the context it which it is being used. 有人声称这是错误的代码,但在不知道其使用上下文的情况下也不是错误的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM