Итерация массива с использованием двух циклов for и вывод значений

Нет, к сожалению, нет; свойство IDENTITY принадлежит таблице, а не столбцу.

Более простой способ сделать это в графическом интерфейсе, но если это не вариант, вы можете пройти длинный путь копирования данных , сбросив столбец, повторно добавив его с идентификатором и вернув данные.

См. здесь для учетной записи «удар по удалению».

2
задан zi zi 19 March 2019 в 14:22
поделиться

2 ответа

Вы можете использовать массив со смещениями для массива для подсчета.

[
    [-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));

0
ответ дан Nina Scholz 19 March 2019 в 14:22
поделиться
<!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>
0
ответ дан Richard Uie 19 March 2019 в 14:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: