I have array like this in JavaScript
我在JavaScript中有这样的数组
var a = [1,2,3,4,5,"4","12","2",6,7,"4",3,"2"];
My questions are
我的问题是
-
How do I split the array to change, (comma) to ";"
如何拆分要更改的数组,(逗号)为“;”
-
How do I return the number of strings in the array as well as return total value regardless of variable type.
如何返回数组中的字符串数以及返回总值而不管变量类型如何。
-
How do I return the average value (regardless of variable type again)
如何返回平均值(不管变量类型如何)
5 个解决方案
#1
1
Answering Question 1
回答问题1
1.Convert it to a string
1.将其转换为字符串
var x=a.toString();
2.Perform a global replace
2.执行全局替换
var y= x.replace(/,/g,";");
3.This gives you "1;2;3;4;5;4;12;2;6;7;4;3;2"
这给你“1; 2; 3; 4; 5; 4; 12; 2; 6; 7; 4; 3; 2”
For Question 2 Simply use the a.length
method.This will give you the total number of elements.I'm not sure about the String elements part.
问题2只需使用a.length方法。这将为您提供元素总数。我不确定String元素部分。
#2
1
Change array separator:
更改数组分隔符:
a.join(';');
as referenced here
这里引用
Number of strings in array:
数组中的字符串数:
var stringCount = 0;
for (var i = 0; i < a.length; i++){
if (typeof a[i] === 'string'){
stringCount++;
}
}
alert("The number of strings is: " + stringCount);
Test it here: https://jsfiddle.net/6c42nugy/2/
在这里测试:https://jsfiddle.net/6c42nugy/2/
Number of all elements in array:
数组中所有元素的数量:
var entireLength = a.length;
Percentage per type in comparison to whole
与整体相比,每种类型的百分比
var stringPerc = (stringCount)/(stringCount + entireLength)
var otherPerc = (entireLength - stringCount)/(stringCount + entireLength)
#3
0
Use a.join(';');
to convert the array into a string separated by semicolons.
使用a.join(';');将数组转换为由分号分隔的字符串。
To return the number of strings in the array, simply iterate through it and use toString.call()
like this:
要返回数组中的字符串数,只需遍历它并使用toString.call(),如下所示:
var total = 0;
for (var i = 0, n = a.length; i < n; i++) {
if (toString.call(a[i]) === '[object String]') total++;
}
To simply return the total number of items, you can do a.length
.
要简单地返回项目总数,您可以执行a.length。
If you want the total value as in all the elements summed, you can do this:
如果你想要总和所有元素中的总值,你可以这样做:
var total = 0;
for (var i = 0, n = a.length; i < n; i++) {
total += parseInt(a[i]);
}
If you want the average, just take the code above and do total / a.length;
.
如果你想要平均值,只需取上面的代码并做total / a.length;。
#4
0
Here is my version:
这是我的版本:
var a = [1,2,3,4,5,"4","12","2",6,7,"4",3,"2"], sum = 0, strings = 0;
var joined = a.join(';');
console.log("Joined: " + joined);
for (var i = 0; i < a.length; i++) {
sum += parseInt(a[i]);
if (typeof a[i] === "string") {
strings++;
}
}
var avg = sum / a.length;
console.log('Sum: ' + sum);
console.log('Average: ' + avg);
console.log('Strings: ' + strings);
And link to JSFiddle.
并链接到JSFiddle。
#5
0
All results in one loop.
所有结果都在一个循环中。
0 ... n-4: Data
n-3: Count of all strings
n-2: Sum
n-1: Average
0 ... n-4:数据n-3:所有字符串的计数n-2:求和n-1:平均值
var data = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"],
result = data.concat(data.reduce(function (r, a, i) {
typeof a === 'string' && r[0]++;
r[1] += +a;
r[2] = r[1] / i;
return r;
}, [0, 0, 0])).join(';');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
#1
1
Answering Question 1
回答问题1
1.Convert it to a string
1.将其转换为字符串
var x=a.toString();
2.Perform a global replace
2.执行全局替换
var y= x.replace(/,/g,";");
3.This gives you "1;2;3;4;5;4;12;2;6;7;4;3;2"
这给你“1; 2; 3; 4; 5; 4; 12; 2; 6; 7; 4; 3; 2”
For Question 2 Simply use the a.length
method.This will give you the total number of elements.I'm not sure about the String elements part.
问题2只需使用a.length方法。这将为您提供元素总数。我不确定String元素部分。
#2
1
Change array separator:
更改数组分隔符:
a.join(';');
as referenced here
这里引用
Number of strings in array:
数组中的字符串数:
var stringCount = 0;
for (var i = 0; i < a.length; i++){
if (typeof a[i] === 'string'){
stringCount++;
}
}
alert("The number of strings is: " + stringCount);
Test it here: https://jsfiddle.net/6c42nugy/2/
在这里测试:https://jsfiddle.net/6c42nugy/2/
Number of all elements in array:
数组中所有元素的数量:
var entireLength = a.length;
Percentage per type in comparison to whole
与整体相比,每种类型的百分比
var stringPerc = (stringCount)/(stringCount + entireLength)
var otherPerc = (entireLength - stringCount)/(stringCount + entireLength)
#3
0
Use a.join(';');
to convert the array into a string separated by semicolons.
使用a.join(';');将数组转换为由分号分隔的字符串。
To return the number of strings in the array, simply iterate through it and use toString.call()
like this:
要返回数组中的字符串数,只需遍历它并使用toString.call(),如下所示:
var total = 0;
for (var i = 0, n = a.length; i < n; i++) {
if (toString.call(a[i]) === '[object String]') total++;
}
To simply return the total number of items, you can do a.length
.
要简单地返回项目总数,您可以执行a.length。
If you want the total value as in all the elements summed, you can do this:
如果你想要总和所有元素中的总值,你可以这样做:
var total = 0;
for (var i = 0, n = a.length; i < n; i++) {
total += parseInt(a[i]);
}
If you want the average, just take the code above and do total / a.length;
.
如果你想要平均值,只需取上面的代码并做total / a.length;。
#4
0
Here is my version:
这是我的版本:
var a = [1,2,3,4,5,"4","12","2",6,7,"4",3,"2"], sum = 0, strings = 0;
var joined = a.join(';');
console.log("Joined: " + joined);
for (var i = 0; i < a.length; i++) {
sum += parseInt(a[i]);
if (typeof a[i] === "string") {
strings++;
}
}
var avg = sum / a.length;
console.log('Sum: ' + sum);
console.log('Average: ' + avg);
console.log('Strings: ' + strings);
And link to JSFiddle.
并链接到JSFiddle。
#5
0
All results in one loop.
所有结果都在一个循环中。
0 ... n-4: Data
n-3: Count of all strings
n-2: Sum
n-1: Average
0 ... n-4:数据n-3:所有字符串的计数n-2:求和n-1:平均值
var data = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"],
result = data.concat(data.reduce(function (r, a, i) {
typeof a === 'string' && r[0]++;
r[1] += +a;
r[2] = r[1] / i;
return r;
}, [0, 0, 0])).join(';');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');