1

Currently putting together a function in my app and was wondering if theres a neater way to write this with ES6 rather than use two for loops.

The aim is create a multi-dimensional array to track co-ordinates x & y. This works fine as it stands but I'm hoping to make it neater.

function setBoard() {
boardParts = new Array(tileCount);
for (let i = 0; i < tileCount; ++i) {
    boardParts[i] = new Array(tileCount);
    for (let j = 0; j < tileCount; ++j) {
        boardParts[i][j] = new Object();
        boardParts[i][j].x = tileCount - 1 - i;
        boardParts[i][j].y = tileCount - 1 - j;
    }
}
emptyLoc.x = boardParts[tileCount - 1][tileCount - 1].x;
emptyLoc.y = boardParts[tileCount - 1][tileCount - 1].y;
solved = false;
}

Appreciate any help!

Thanks

2
  • 2
    There are object literals even in ES5 :-) Commented Sep 19, 2017 at 17:53
  • Also, emptyLoc.x = emptyLoc.y = 0; - those are always the same? Commented Sep 19, 2017 at 17:56

2 Answers 2

2

If you want to go ES6, you can use Array#from to generate the arrays:

const tileCount = 4;

const boardParts = Array.from({ length: tileCount }, (_, i) => 
  Array.from({ length: tileCount }, (_, j) => ({
    x: tileCount - 1 - i,
    y: tileCount - 1 - j
  }))
);

console.log(boardParts);

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, fair enough, there is something in ES2015 that relates to this. I wouldn't say it's an improvement necessarily, but the OP did specifically ask... :-)
@T.J.Crowder - Array#from is the closest JS gets to python's list comprehensions, and it's a pity that the array comprehensions are not supported in JS yet.
1

There's nothing particularly in ES2015+ that helps with that (well, other than Array.from as Ori Drori points out, which you may or may not want), but there are several things you can do to improve it that were also available in ES5, see comments:

function setBoard() {
    boardParts = []; // No need to create with initial `length`, generic arrays
                     // aren't really arrays, no pre-allocation necessary
    for (let i = 0; i < tileCount; ++i) {
        boardParts[i] = []; // See above
        for (let j = 0; j < tileCount; ++j) {
            boardParts[i][j] = {           // Object initializer rather than
                x: tileCount - 1 - i,      // `new Object` and then prop
                y: tileCount - 1 - j       // assignment
            };
        }
    }
    emptyLoc.x = boardParts[tileCount - 1][tileCount - 1].x;
    emptyLoc.y = boardParts[tileCount - 1][tileCount - 1].y;
    solved = false;
}

Separately to the above: The function presented expects to find boardParts, tileCount, emptyLoc, and solved declared in a containing context. Normally it's not ideal to have functions that purely work through side-effects unless they're object initializers of some kind...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.