使用两个 for 循环进行数组迭代并打印值
Array iteration by using two for loops and print the values
更新 2:
现在我们切片时会得到16个小数组,我们需要将所有16个小数组相加并找出哪个更大。
所以我添加了两个 for 循环来迭代小数组集并调试代码,但不是打印 16 次,而是打印 27 次。
你能告诉我有什么问题吗?在下面提供我更新的代码片段
第一组数组:
+------------+ --- outerIndex
| 0 0 0 | 0 0 0 |
| | |
| 5 5 5 | 0 0 0 |
| | \|/ outerSize
|10 10 10 | 0 0 0 ---
+------------+
0 0 0 0 0 0
5 5 5 0 0 0
10 10 10 0 0 0
第二组数组:
+--------------+ --- outerIndex
0 | 0 0 0 | 0 0 |
| | |
5 | 5 5 0 | 0 0 |
| | \|/ outerSize
10 | 10 10 0 | 0 0 ---
+--------------+
0 0 0 0 0 0
5 5 5 0 0 0
10 10 10 0 0 0
function getSum(array) {
return array.reduce((s, v) => s + (Array.isArray(v) ? getSum(v) : v), 0);
}
function getSub2D(array, outerIndex, outerSize, innerIndex, innerSize) {
return array
.slice(outerIndex, outerIndex + outerSize)
.map(a => a.slice(innerIndex, innerIndex + innerSize));
}
function hourglassSum(arr) {
let array = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]];
let twoDimensionalAdded = 0;
for (let outerLoop = 0; outerLoop < array.length; outerLoop++) {
// console.log("array.length--->", array.length);
for (let innerLoop = 0; innerLoop < array[outerLoop].length; innerLoop++) {
//console.log("array[outerLoop].length--->", array[outerLoop].length);
console.log("outerLoop--->", outerLoop);
console.log("innerLoop--->", innerLoop);
let sub = getSub2D(array, outerLoop, 3, innerLoop, 3);
let total = getSum(sub);
sub.forEach(a => console.log("getSub2D--->", ...a));
console.log("adding subset of whole array--->", total);
console.log("array[outerLoop][innerLoop]--->", array[outerLoop][innerLoop]);
// twoDimensionalAdded += array[outerLoop][innerLoop]; // add item to sum
}
}
// console.log("twoDimensionalAdded--->", twoDimensionalAdded);
// let sub = getSub2D(array, outerLoop, 3, innerLoop, 3);
// let sub = getSub2D(array, 0, 3, 0, 3);
// let total = getSum(sub);
// sub.forEach(a => console.log(...a));
// console.log(total);
}
更新1:
嗨,
现在我可以在数组中添加元素并打印值,但为了解决我的问题,我需要单独添加 dimensionalTwo[0][1], dimensionalTwo[0][2], dimensionalTwo[0][3], dimensionalTwo[1][1], dimensionalTwo[1][2], dimensionalTwo[1][3], dimensionalTwo[2][1], dimensionalTwo[2][2], dimensionalTwo[2][3]
的值并打印结果,在下面提供更新的代码片段
function adding particularSet(arr) {
let dimensionalTwo = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]];
let twoDimensionalAdded = 0;
for (let outerLoop = 0; outerLoop < dimensionalTwo.length; outerLoop++) {
for (let innerLoop = 0; innerLoop < dimensionalTwo[outerLoop].length; innerLoop++) {
console.log("dimensionalTwo[outerLoop][innerLoop]--->", dimensionalTwo[outerLoop][innerLoop]);
twoDimensionalAdded += dimensionalTwo[outerLoop][innerLoop]; // add item to sum
}
}
console.log("twoDimensionalAdded--->", twoDimensionalAdded);
}
- 我是 js 新手。
- 我正在尝试迭代一个二维数组并添加值然后打印它。
- 但问题是我无法打印这些值。
- 如果你告诉我就太好了。
- 这样我就可以迭代我的数组并为我未来的任务打印结果。
- 在下面提供我的代码片段
var DimensionalTwo = [
[0, 0, 0],
[5, 5, 5],
[10, 10, 10]
];
for (let outerLoop = 0; outerLoop < DimensionalTwo.length; outerLoop++) {
for (let innerLoop = 0; innerLoop < DimensionalTwo[outerLoop].length; innerLoop++) {
console.log("DimensionalTwo[outerLoop][innerLoop]--->", DimensionalTwo[outerLoop][innerLoop]);
let TwoDimensonalTemporayStorage = DimensionalTwo[outerLoop][innerLoop];
let twoDimensionalAdded = TwoDimensonalTemporayStorage + DimensionalTwo[outerLoop][innerLoop];
console.log("twoDimensionalAdded--->", twoDimensionalAdded);
}
//console.log("inner for loop twoDimensionalAdded--->", twoDimensionalAdded);
}
您可以使用带有数组偏移量的数组进行计数。
[
[-1, -1], [-1, 0], [-1, 1],
[ 0, 0],
[ 1, -1], [ 1, 0], [ 1, 1]
]
var array = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]],
hourglass = [[-1, -1], [-1, 0], [-1, 1], [0, 0], [1, -1], [1, 0], [1, 1]],
totals = array.map(a => a.map(_ => 0)), // get an array with all zero values
i, j, k;
for (i = 1; i < array.length - 1; i++) {
for (j = 1; j < array[i].length - 1; j++) {
totals[i][j] = 0;
for (k = 0; k < hourglass.length; k++) {
totals[i][j] += array[i + hourglass[k][0]][j + hourglass[k][1]];
}
}
}
totals.forEach(a => console.log(...a));
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<style type="text/css">
</style>
<script type="text/javascript">
/*
Let's simply represent this entire problem space differently.
Let's reconceive homogeneous, higher-order, JS arrays as two-
dimensional, algebraic arrays instead of JS arrays, since JS
is not natively well-suited for this.
First, a few utility functions for handling arrays...
*/
// calculate and return the sums of all the desired subarrays -
// relies on functions that treat JS arrays *differently*;
// current implementations assumes that square subarrays of shape
// {size x size} are desired
function calcSubSums(arr, size) {
var sums = new Array();
for (var i = 0; i < arr.length - size + 1; i++) {
for (var j = 0; j < arr[i].length - size + 1; j++) {
sums.push(reduce(ravel(getSubArray(arr,[i,j],size))));
}
}
return sums;
};
// for an array, arr, return subarray that starts at the top-left-
// corner indexes tlc (top-row-index, left-column-index) for an
// extent of size on each dimension
function getSubArray(arr, tlc, size) {
var a = new Array();
for (var i = tlc[0]; i < size + tlc[0]; i++) {
var b = new Array();
for (var j = tlc[1]; j < size + tlc[1]; j++) {
b.push(arr[i][j]);
}
a.push(b);
}
return a;
};
// convert a higher dimensional array into one-dimensional array
// that contains all of its elements, unpacking from top-to-bottom,
// left-to-right
function ravel(arr, flat) {
// If flat - accumulator array - not yet defined, create it.
if ('undefined' == typeof flat) flat = new Array();
// If arg is an array, iterate over the elements in index order.
if (isArray(arr)) {
// Call self recursively to get elements or process next,
// outermost level of nesting.
for (var i = 0; i < arr.length; i++) { ravel(arr[i], flat); }
}
// Otherwise, just add simple element to accumulator.
else flat.push( arr );
// Return accumulated values.
return flat;
};
// return a Boolean indicator of whether the argument is a JS array
function isArray(a) {
if ('undefined' == typeof a) {return false};
return -1 != a.constructor.toString().indexOf('Array');
};
// place the operator {op} between the elements of a and evaluate the
// entire, resulting expression
function reduce(a, op) {
// Set default op (add/concatenate), if not given.
if ('undefined' == typeof op) op = '+';
// Initialize command to evaluate.
var cmd = '';
// Compose command string - concatenate each element with op.
for (var i = 0; i < a.length; i++) {cmd += a[i] + op;}
// Remove, extraneous, trailing instance of op and return evaluation.
return eval(cmd.substring(0, cmd.length - op.length));
};
// now let's test it...
window.onload = function() {
// declare the test array
var array = [
[ 0, 0, 0, 0, 0, 0],
[ 5, 5, 5, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 5, 5, 5, 0, 0, 0],
[10, 10, 10, 0, 0, 0]
];
// calculate all of the sums of 3x3 subset arrays of our test array
// and write the totals to the console
console.log(calcSubSums(array, 3));
};
</script>
</head>
<body></body>
</html>
更新 2:
现在我们切片时会得到16个小数组,我们需要将所有16个小数组相加并找出哪个更大。 所以我添加了两个 for 循环来迭代小数组集并调试代码,但不是打印 16 次,而是打印 27 次。 你能告诉我有什么问题吗?在下面提供我更新的代码片段
第一组数组:
+------------+ --- outerIndex | 0 0 0 | 0 0 0 | | | | | 5 5 5 | 0 0 0 | | | \|/ outerSize |10 10 10 | 0 0 0 --- +------------+ 0 0 0 0 0 0 5 5 5 0 0 0 10 10 10 0 0 0
第二组数组:
+--------------+ --- outerIndex 0 | 0 0 0 | 0 0 | | | | 5 | 5 5 0 | 0 0 | | | \|/ outerSize 10 | 10 10 0 | 0 0 --- +--------------+ 0 0 0 0 0 0 5 5 5 0 0 0 10 10 10 0 0 0
function getSum(array) {
return array.reduce((s, v) => s + (Array.isArray(v) ? getSum(v) : v), 0);
}
function getSub2D(array, outerIndex, outerSize, innerIndex, innerSize) {
return array
.slice(outerIndex, outerIndex + outerSize)
.map(a => a.slice(innerIndex, innerIndex + innerSize));
}
function hourglassSum(arr) {
let array = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]];
let twoDimensionalAdded = 0;
for (let outerLoop = 0; outerLoop < array.length; outerLoop++) {
// console.log("array.length--->", array.length);
for (let innerLoop = 0; innerLoop < array[outerLoop].length; innerLoop++) {
//console.log("array[outerLoop].length--->", array[outerLoop].length);
console.log("outerLoop--->", outerLoop);
console.log("innerLoop--->", innerLoop);
let sub = getSub2D(array, outerLoop, 3, innerLoop, 3);
let total = getSum(sub);
sub.forEach(a => console.log("getSub2D--->", ...a));
console.log("adding subset of whole array--->", total);
console.log("array[outerLoop][innerLoop]--->", array[outerLoop][innerLoop]);
// twoDimensionalAdded += array[outerLoop][innerLoop]; // add item to sum
}
}
// console.log("twoDimensionalAdded--->", twoDimensionalAdded);
// let sub = getSub2D(array, outerLoop, 3, innerLoop, 3);
// let sub = getSub2D(array, 0, 3, 0, 3);
// let total = getSum(sub);
// sub.forEach(a => console.log(...a));
// console.log(total);
}
更新1:
嗨,
现在我可以在数组中添加元素并打印值,但为了解决我的问题,我需要单独添加 dimensionalTwo[0][1], dimensionalTwo[0][2], dimensionalTwo[0][3], dimensionalTwo[1][1], dimensionalTwo[1][2], dimensionalTwo[1][3], dimensionalTwo[2][1], dimensionalTwo[2][2], dimensionalTwo[2][3]
的值并打印结果,在下面提供更新的代码片段
function adding particularSet(arr) {
let dimensionalTwo = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]];
let twoDimensionalAdded = 0;
for (let outerLoop = 0; outerLoop < dimensionalTwo.length; outerLoop++) {
for (let innerLoop = 0; innerLoop < dimensionalTwo[outerLoop].length; innerLoop++) {
console.log("dimensionalTwo[outerLoop][innerLoop]--->", dimensionalTwo[outerLoop][innerLoop]);
twoDimensionalAdded += dimensionalTwo[outerLoop][innerLoop]; // add item to sum
}
}
console.log("twoDimensionalAdded--->", twoDimensionalAdded);
}
- 我是 js 新手。
- 我正在尝试迭代一个二维数组并添加值然后打印它。
- 但问题是我无法打印这些值。
- 如果你告诉我就太好了。
- 这样我就可以迭代我的数组并为我未来的任务打印结果。
- 在下面提供我的代码片段
var DimensionalTwo = [
[0, 0, 0],
[5, 5, 5],
[10, 10, 10]
];
for (let outerLoop = 0; outerLoop < DimensionalTwo.length; outerLoop++) {
for (let innerLoop = 0; innerLoop < DimensionalTwo[outerLoop].length; innerLoop++) {
console.log("DimensionalTwo[outerLoop][innerLoop]--->", DimensionalTwo[outerLoop][innerLoop]);
let TwoDimensonalTemporayStorage = DimensionalTwo[outerLoop][innerLoop];
let twoDimensionalAdded = TwoDimensonalTemporayStorage + DimensionalTwo[outerLoop][innerLoop];
console.log("twoDimensionalAdded--->", twoDimensionalAdded);
}
//console.log("inner for loop twoDimensionalAdded--->", twoDimensionalAdded);
}
您可以使用带有数组偏移量的数组进行计数。
[
[-1, -1], [-1, 0], [-1, 1],
[ 0, 0],
[ 1, -1], [ 1, 0], [ 1, 1]
]
var array = [[0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0], [0, 0, 0, 0, 0, 0], [5, 5, 5, 0, 0, 0], [10, 10, 10, 0, 0, 0]],
hourglass = [[-1, -1], [-1, 0], [-1, 1], [0, 0], [1, -1], [1, 0], [1, 1]],
totals = array.map(a => a.map(_ => 0)), // get an array with all zero values
i, j, k;
for (i = 1; i < array.length - 1; i++) {
for (j = 1; j < array[i].length - 1; j++) {
totals[i][j] = 0;
for (k = 0; k < hourglass.length; k++) {
totals[i][j] += array[i + hourglass[k][0]][j + hourglass[k][1]];
}
}
}
totals.forEach(a => console.log(...a));
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<style type="text/css">
</style>
<script type="text/javascript">
/*
Let's simply represent this entire problem space differently.
Let's reconceive homogeneous, higher-order, JS arrays as two-
dimensional, algebraic arrays instead of JS arrays, since JS
is not natively well-suited for this.
First, a few utility functions for handling arrays...
*/
// calculate and return the sums of all the desired subarrays -
// relies on functions that treat JS arrays *differently*;
// current implementations assumes that square subarrays of shape
// {size x size} are desired
function calcSubSums(arr, size) {
var sums = new Array();
for (var i = 0; i < arr.length - size + 1; i++) {
for (var j = 0; j < arr[i].length - size + 1; j++) {
sums.push(reduce(ravel(getSubArray(arr,[i,j],size))));
}
}
return sums;
};
// for an array, arr, return subarray that starts at the top-left-
// corner indexes tlc (top-row-index, left-column-index) for an
// extent of size on each dimension
function getSubArray(arr, tlc, size) {
var a = new Array();
for (var i = tlc[0]; i < size + tlc[0]; i++) {
var b = new Array();
for (var j = tlc[1]; j < size + tlc[1]; j++) {
b.push(arr[i][j]);
}
a.push(b);
}
return a;
};
// convert a higher dimensional array into one-dimensional array
// that contains all of its elements, unpacking from top-to-bottom,
// left-to-right
function ravel(arr, flat) {
// If flat - accumulator array - not yet defined, create it.
if ('undefined' == typeof flat) flat = new Array();
// If arg is an array, iterate over the elements in index order.
if (isArray(arr)) {
// Call self recursively to get elements or process next,
// outermost level of nesting.
for (var i = 0; i < arr.length; i++) { ravel(arr[i], flat); }
}
// Otherwise, just add simple element to accumulator.
else flat.push( arr );
// Return accumulated values.
return flat;
};
// return a Boolean indicator of whether the argument is a JS array
function isArray(a) {
if ('undefined' == typeof a) {return false};
return -1 != a.constructor.toString().indexOf('Array');
};
// place the operator {op} between the elements of a and evaluate the
// entire, resulting expression
function reduce(a, op) {
// Set default op (add/concatenate), if not given.
if ('undefined' == typeof op) op = '+';
// Initialize command to evaluate.
var cmd = '';
// Compose command string - concatenate each element with op.
for (var i = 0; i < a.length; i++) {cmd += a[i] + op;}
// Remove, extraneous, trailing instance of op and return evaluation.
return eval(cmd.substring(0, cmd.length - op.length));
};
// now let's test it...
window.onload = function() {
// declare the test array
var array = [
[ 0, 0, 0, 0, 0, 0],
[ 5, 5, 5, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 5, 5, 5, 0, 0, 0],
[10, 10, 10, 0, 0, 0]
];
// calculate all of the sums of 3x3 subset arrays of our test array
// and write the totals to the console
console.log(calcSubSums(array, 3));
};
</script>
</head>
<body></body>
</html>