Js uses foreach to traverse, and then style.marginLeft dom to find that it is useless, ordinary for loops can, why?

(function(){
    var child_arr = document.getElementsByClassName("child");
    var len = child_arr.length;
    var parent = document.getElementsByClassName("parent")[0];
    var move_width = parent.offsetWidth;
    var index = 0; // div
    function animate() {
        var childArr = [].slice.call(child_arr);
        if (index === len) {
            index = 0;
        }
        childArr.forEach(function(item, key) {
            if (index - key >= 0) {
                child_arr[key].style.marginLeft = "-" + move_width * (index - key) + "px";
            } else {
                child_arr[key].style.marginLeft = move_width * (key - index) + "px";
            }
        })
        indexPP;
    }
    clearInterval(t);
    var t = setInterval(animate, 1000);
})()

I can"t write this way. Why? I still operate on Dom elements. Does anyone know why? Ask for advice

Mar.16,2021

k ik where are these two defined. Whether the property name has not been changed, the console should have reported an error. What you want to write is key bar


well, I'm an idiot.
I found that a semicolon was added to the style assignment in the local code, but the code given here did not add a semicolon, so maybe I didn't measure it carefully.
the local code is actually:

child_arr[key].style.marginLeft = '-' + move_width * (index - key) + 'px;' // 

you just need to delete the semicolon.
23333333333333333333333333

Menu