[英]Javascript array return is adding double quotes?
Here is my code: 这是我的代码:
function iLoveThree (array) {
var threes = [];
var x;
for (x in array) {
if (x % 3 == 0) {
threes.push(x)
}
}
return threes
}
When I pass the array [1,2,3,4,5,6,7,8,9] I get the following: 当我传递数组[1,2,3,4,5,6,7,8,9]时,我得到以下信息:
Function returned
["0","3","6"]
instead of
[3,6,9]
My question is, where are these double quotes coming from? 我的问题是,这些双引号来自哪里?
for...in
is a bad way of iterating array indices. for...in
是迭代数组索引的一种坏方法。 Better use filter
: 最好使用filter
:
[1,2,3,4,5,6,7,8,9].filter(function(x) {
return x % 3 == 0;
}); // [3, 6, 9]
So basically in x in array
x
is the index not the array value. 因此,基本上在x in array
x
中的x
中,索引不是数组值。 Because anyway 0
is not in the array but your function is returning it as well. 因为无论如何0
不在数组中,但是您的函数也会返回它。 You should instead access the values using array[x]
您应该改用array[x]
访问值
There are various approaches, one of them is using .filter
有多种方法,其中一种是使用.filter
function iLoveThree(array){
return array.filter(function(x){
return (x%3==0?1:0);
});
}
Or 要么
function iLoveThree (array) {
var threes = [];
var x;
[].forEach.call(array, function(x){
if (x % 3 == 0) {
threes.push(x)
}
}
return threes
}
You're using a for..in
loop which gives you the keys in an object, or in this case and array. 您正在使用for..in
循环,该循环为您提供了对象(或本例中的数组)中的键。 Keys are always strings. 键始终是字符串。 Instead, you want to use a standard for
loop. 相反,您想使用标准的for
循环。
for (var i = 0; i < array.length; i++) {
var x = array[i];
if (x % 3 === 0) {
threes.push(x);
}
}
Or if you want to use a more functional approach, you could use Array.prototype.filter . 或者,如果您想使用更多功能的方法,则可以使用Array.prototype.filter 。
return array.filter(function(x) {
return x % 3 === 0;
});
A for..in
loop does not loop through the array elements, it loops through the indices of the array. 甲for..in
循环不循环通过数组元素,它循环通过该阵列的索引。 So for: 因此对于:
var arr = ["a", "b", "c"]
for ( x in arr ) console.log( x );
You'll get the string keys of ["0", "1", "2"]
您将获得["0", "1", "2"]
的字符串键
You can fix your code by replacing your loop with a native for
loop: 您可以通过将循环替换为本地for
循环来修复代码:
for ( var x = 0; x < array.length; x++ ) {
if (array[i] % 3 == 0)
threes.push(array[i]);
}
It seems that you are pushing in the indexes and not the actual values, go ahead and try the following: 看来您正在推送索引而不是实际值,请继续尝试以下操作:
function iLoveThree(array) {
var threes = [];
var x;
for (x in array) {
if (((x-2) % 3) == 0) {
threes.push(array[x])
}
}
return threes;
}
Another option, shorter, is: 另一个简短的选择是:
function iLoveThree(arr) {
var threes = [];
for (var i = 2; i < arr.length; i = i + 3) {
threes.push(arr[i]);
};
return threes;
}
if you are comfortable with callback/predicate based loops, you could make stuff even shorter by filtering the array, instead of creating a new one: 如果您对基于回调/谓词的循环感到满意,则可以通过过滤数组而不是创建一个新数组来使事情变得更短:
function iLoveThree(arr) {
return arr.filter(function(x) {
return (x % 3) == 0;
});
}
不是直接回答您的问题,而是一种从数字数组中提取3的每一个倍数的好方法:
[1,2,3,4,5,6,7,8,9].filter(item => item % 3 === 0)
Before you read the answer below, please read: Why is using “for…in” with array iteration such a bad idea? 在阅读下面的答案之前,请阅读: 为什么在数组迭代中使用“ for…in”这么一个坏主意? (Thanks to @Oriol for this link.) (感谢@Oriol提供此链接。)
Douglas Crockford has also discouraged the use of for..in
. 道格拉斯·克罗克福德(Douglas Crockford)也不鼓励for..in
的使用。 He recommends using array.forEach
instead. 他建议改为使用array.forEach
。
function iLoveThree (array) { var threes = []; array.forEach(function(item, i){ // use forEach, 'item' is the actual value and 'i' is the index if (item % 3 === 0) { threes.push(item); // you missed ; here } }); return threes; // you missed ; here } console.log(iLoveThree([1,2,3,4,5,6,7,8,9]));
Read up: Array.prototype.forEach()
| 阅读: Array.prototype.forEach()
| MDN MDN
If you read the for...in documentation, you will realize that you are pushing to threes
the indexes (also called keys ) not the values , because the variable x
represents the index, so the value should be accessed by array[x]
. 如果您阅读文档中的for ... ,您将意识到将索引 (也称为keys )而不是值推到threes
,因为变量x
表示索引,因此应该由array[x]
访问该值。 。
function iLoveThree (array) {
var threes = [];
for (var x in array) {
if (array[x] % 3 == 0) {
threes.push(array[x])
}
}
return threes
}
There are several ways to achieve this, the best one is by using a filter
, but that way was already explained by someone else, therefore I will use an exotic implementation using a reduce
有几种方法可以做到这一点,最好的方法是使用filter
,但是其他人已经解释了这种方法,因此我将使用带有reduce
的奇异实现
[1, 2, 3, 4, 5, 6, 7, 8, 9].reduce(function(acc, e){return e % 3 == 0 ? acc.concat(e) : acc}, [])
Outputs 3, 6, 9
输出3, 6, 9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.