我是一名学习JavaScript的学生,所以我的知识并不复杂,所以请保持温和。 :)
我得到了:
Add-on
括号中的每个数字代表该季度的数字。我需要给出循环来总结每个区域(加法)和每个区域(在列中添加)的销售额。我理解如何做单项数组。例如:
var region1 = [1540, 1130, 1580, 1105];
var region2 = [2010, 1168, 2305, 4102];
var region3 = [2450, 1847, 2710, 2391];
var region4 = [1845, 1491, 1284, 1575];
var region5 = [2120, 1767, 1599, 3888];
先谢谢你。
答案 0 :(得分:2)
我不确定这是不是你想要的,但我认为你想要在所有五个地区的总销售额为四分之一;
{
quarter_1 : 6000,
quarter_2 : 5000,
quarter_3 : 7000,
quarter_4 : 5600
}
答案 1 :(得分:1)
这有两个功能可以做我认为你要问的事情。第一个区域将一个区域作为参数,并输出其数字的总和。
第二个需要两个参数 - 你喜欢它的总和,以及一个包含所有区域的数组。
var region1 = [1540, 1130, 1580, 1105];
var region2 = [2010, 1168, 2305, 4102];
var region3 = [2450, 1847, 2710, 2391];
var region4 = [1845, 1491, 1284, 1575];
var region5 = [2120, 1767, 1599, 3888];
var allRegions = [region1, region2, region3, region4, region5];
function getRegionTotal(region) {
var total = 0;
for (var i = 0; i < region.length; i++) {
total += region[i];
}
return total;
}
var region1Total = getRegionTotal(region1);
console.log('Region 1 total:', region1Total);
function getQuarterTotal(quarter, regions) {
var total = 0;
for (var i = 0; i < regions.length; i++) {
// Note that we reduce the quarter variable by one. This is due to arrays being zero-based.
total += regions[i][quarter - 1];
}
return total;
}
var quarter2Total = getQuarterTotal(2, allRegions);
console.log('Quarter 2 total:', quarter2Total);
&#13;
答案 2 :(得分:1)
我认为首先应该获得一个数组数组:
var region1 = [1540, 1130, 1580, 1105];
var region2 = [2010, 1168, 2305, 4102];
var region3 = [2450, 1847, 2710, 2391];
var region4 = [1845, 1491, 1284, 1575];
var region5 = [2120, 1767, 1599, 3888];
var rawArr = [];
rawArr.push(region1);
rawArr.push(region2);
rawArr.push(region3);
rawArr.push(region4);
rawArr.push(region5);
然后,您将能够处理添加列和添加列:
var summRegion = rawArr.map((element) => element.reduce((a, b)=> a + b,0));
console.log(summRegion); // [ 5355, 9585, 9398, 6195, 9374 ]
var sumColumns = [0,0,0,0];
for (var i = 0; i < 4; i++)
rawArr.forEach((element) => {sumColumns[i] += element[i]});
console.log(sumColumns); // [ 9965, 7403, 9478, 13061 ]
答案 3 :(得分:0)
你的意思是这样吗?
$all_posts = $wpdb->get_results( "
SELECT *, pll__(meta_value) as meta_value_pl
FROM $posts
LEFT JOIN $postmeta
ON ( $posts.ID = $postmeta.post_id )
WHERE post_type = 'company' AND post_status = 'publish' AND $postmeta.meta_key = '$name' $where_search
ORDER BY meta_value DESC
LIMIT $start, $per_page");
之后你将拥有一个像这样结构的对象:
var quarterlyResultsObj = {};
for( var x = 0 ; x < region1.length ; x++ ){
var thisQuarter = "quarter_" + x;
var quarterResult = region1[x] + region2[x] + region3[x] + region4[x] + region5[x];
quarterlyResultsObj[ thisQuarter ] = quarterResult;
}
答案 4 :(得分:0)
我包括季度和地区的计算。 可以在https://jsfiddle.net/d8hLc9L5/5/中找到工作示例。
var regions = [
[1540, 1130, 1580, 1105],
[2010, 1168, 2305, 4102],
[2450, 1847, 2710, 2391],
[1845, 1491, 1284, 1575],
[2120, 1767, 1599, 3888]
],
countRegions = regions.length,
countQuarter = 4;
function SumQuarter() {
for (var q = 0; q < countQuarter; q++) {
var sumQuarter = 0;
for (var r = 0; r < countRegions; r++) {
sumQuarter = sumQuarter + regions[r][q];
}
console.log('quarter:' + (q + 1) + ' sum: ' + sumQuarter);
}
}
function SumRegions() {
for (var r = 0; r < countRegions; r++) {
var sumRegions = 0;
for (var q = 0; q < countQuarter; q++) {
sumRegions += regions[r][q];
}
console.log('region:' + (r + 1) + ' sum: ' + sumRegions);
}
}
SumQuarter();
SumRegions();