So it seems to me that there are a few things going on here. First, you want to be able to take a single shape in, and output some information about that shape. OR you might get an array of shapes in, and be able to output that same "single-shape" information for each of those shapes.
It is do-able, but the approach is a little different. First, consider defining the function to handle a single shape array:
const displayShapeInfo = (arr) => {
return `--------------------------
The number of endpoints is ${arr.length}
those endpoints: [${arr.join('],[')}]`;
}
This is simply outputting the value as a string, but we could tell that function to return DOM elements, or return whatever. The point is, this function takes in an array like [[1,1],[2,2],[3,3]]
and outputs something like:
-------------------------------------
The number of endpoints is 3
those endpoints: [1,1],[2,2],[3,3]
It takes in a 2-deep array, and outputs information for that array.
In the event of an array of shape-arrays, we have a 3-deep array. From the root, the furthest in we can go is three arrays: the main array (outermost), the shape (mid-level), and the point (innermost). So how can we apply our single-shape-handling function in that case?
In that case, we simply want to say this:
const displayShapes = (arrayOfShapes) => {
let info = []
for(let i=0; i<arrayOfShapes.length; i++){
info.push( displayShapeInfo(arrayOfShapes[i]) );
}
return info;
}
That one takes a three-deep array, and applies our function to each two-deep array within the top level. And yes, for those who look for completeness, this could be done with arrayOfShapes.map(displayShapeInfo)
, but that might be confusing.
And the next bit will be confusing enough. See, we still don't know if we are dealing with a two-level-deep array, or a three-level-deep array. We need to getArrayDepth
somehow, telling us the maximum depth of this array. That will determine which way we want to go.
So a little googling for finding array depth in javascript
showed me this one:
const getArrayDepth = (arr) => {
// if arr is an array, recurse over it
return Array.isArray(arr) ?
1 + Math.max(...arr.map(getArrayDepth)) :
0; // otherwise, it's 0
}
That function starts from the outermost array and, if it is an array, runs this same function on each of its children, adding 1 for each recursive call. It will, in short, tell us the maximum depth of each nested array.
If we know that, we can then handle both cases:
const displayShapeInfo = (arr) => {
return `--------------------------
The number of endpoints is ${arr.length}
those endpoints: [${arr.join('],[')}]`;
}
const displayShapes = (arr) => {
switch(getArrayDepth(arr)){
case 2:
// if this is a two-deep array, it is a *shape* array.
return displayShapeInfo(arr);
case 3:
// if it's three-deep, it is an *array of shape arrays*.
let info = [];
for(let i=0; i<arrayOfShapes.length; i++){
info.push( displayShapeInfo(arrayOfShapes[i]) );
}
return info;
default:
// *anything else*, we haven't specified how to handle
return 'Unknown shapes array';
}
}
To see this working, https://replit.com/@TobiasParent/arraysOfShapes#index.js