Javascript / angular - Scrolling an array back using forEach

Is there a way to scroll back through the array using forEach(and not some other type of loop, I know how to do it using standard / standard methods) and without actually accessing the array itself?

+14
source share
7 answers
var arr = [1, 2, 3];

arr.slice().reverse().forEach(function(x) {
    console.log(x);
})

will print:

3
2
1

arrstill will [1, 2, 3], .slice()creates a shallow copy.

+25
source

No, it forEachonly processes an array through an array. So you will need to do something else that you said in your question was out of scope.

, forEach ( for ). , , :

  • , forEach

  • Object.keys, , , forEach ( , , )

# 1:

slice ( , ), , forEach:

var a = ['one', 'two', 'three'];
a.slice().reverse().forEach(function(entry) {
    snippet.log(entry);
});
snippet.log("Proof that a is not, itself, reversed: " +
            JSON.stringify(a));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
Hide result

# 2:

Object.keys ( filter, ), , :

var a = ['one', 'two', 'three'];
Object.keys(a).reverse().forEach(function(index) {
    snippet.log(a[index]);
});
snippet.log("Proof that a is not, itself, reversed: " +
            JSON.stringify(a));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
Hide result

. filter, :

var a = ['one', 'two', 'three'];
a.nonElementProperty = "foo";
Object.keys(a).filter(function(name) {
  return String(+name) === name;
}).reverse().forEach(function(index) {
    snippet.log(a[index]);
});
snippet.log("Proof that a is not, itself, reversed: " +
            JSON.stringify(a));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
Hide result
+6

, forEach ( ES6)

var someArray = ["a","b","c","d"];
someArray.reverse().forEach(arrayItem =>
    console.log(arrayItem)
)

ES6, , .

var someArray = ["a","b","c","d"];
someArray.reverse().forEach(function(arrayItem) {
    console.log(arrayItem)
})

:

d
c    
b
a
+4

, , Array.forEach. polyfill, Array.forEach, , 10 1.

, Array.revEach Array.forEach, , , Array.forEach polyfill , .

Array.revEach out Array.forEach, 17 "Chrome 46.0.2490.22 beta-m"

if (Array.prototype.revEach === undefined) { 
    Object.defineProperty(Array.prototype, 'revEach', { 
        writable : false,
        enumerable : false,
        configurable : false,
        value : function (func) {
            var i;
            var len = this.length-1;
            for (i = len; i >= 0; i--) {
                func(this[i], i, this);
            }
        }
    });
}

polyfill, . .

// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.imtqy.com/#x15.4.4.18
// Modified by Blindman67 to revEach
if (!Array.prototype.revEach) {   // name changed
  Array.prototype.revEach = function(callback, thisArg) { // name changed
    var T;  // k defined where len was
    if (this == null) {
      throw new TypeError(' this is null or not defined');
    }
    var O = Object(this);
    var k = (O.length >>> 0)-1;  // set k (counter) ToUint32
                                 // len var removed
    if (typeof callback !== "function") {
      throw new TypeError(callback + ' is not a function');
    }
    if (arguments.length > 1) {
      T = thisArg;
    }
    while (k >= 0) {  // reverse condition
      var kValue;
      if (k in O) {
        kValue = O[k];
        callback.call(T, kValue, k, O);
      }
      k--;  // dec counter
    }
  };
}
+2

array.forEach 3 . , forEach .

var arr = [1, 2, 3];

    arr.forEach(function(x, index, the_array) {
        let x_prime = the_array[the_array.length-1-index]
        console.log(x_prime);
    })

3
2
1
0

. .

const array = ['blastoff', 1, 2, 3];

for (let index = array.length - 1; index >= 0; index--) {
  const element = array[index];
  console.log(element);
}
Hide result
0
source

There is a similar array method, which has a back counter part, reducecomes along with : reduceRight

const array = ['alpha', 'beta', 'gamma'];

array.reduceRight((_, elem) => console.log(elem), null);
Run codeHide result

When using it for the requested purpose, be sure to specify the second argument. It could be nullor something else. Also note that the callback function has a battery as the first argument that you do not need for this purpose.

0
source

Source: https://habr.com/ru/post/1608228/


All Articles