I found the cause. There is a module that deep clones complex objects so they can be restored in the future. Can you spot the error in this recursive loop?
function cloneObject(src) {
let target = {};
target.__proto__ = src.__proto__;
for (let prop in src) {
if (src.hasOwnProperty(prop)) {
// if the value is a nested object, recursively copy all it's properties
if (isObject(src[prop])) {
target[prop] = cloneObject(src[prop]);
} else {
target[prop] = src[prop];
}
}
}
return target;
}
When someArray goes through the cloneObject() function, it creates a new object called Array that does not retain true array properties but instead converts it into an object with proto='Array' (as @CRice pointed out) and nothing more. To fix this we really need to rework the cloneObject function to preserve arrays. This is the corrected deep copy cloneObject function I am now using:
function cloneObject(src) {
let target = {};
var isArray = Array.isArray(src);
if(isArray){
target = [];
} else {
target.__proto__ = src.__proto__;
}
for (let prop in src) {
if (src.hasOwnProperty(prop)) {
// if the value is a nested object, recursively copy all it's properties
if (isObject(src[prop])) {
var propertyValue = cloneObject(src[prop]);
} else {
var propertyValue = src[prop];
}
// if src was an array
if(isArray){
target.splice(prop, 0, propertyValue);
} else {
target[prop] = propertyValue;
}
}
}
return target;
}
Answer from Zachary on Stack OverflowAre there different types of Arrays in JavaScript? - Stack Overflow
Why Javascript returns "Array" Data structure as Object. Please explain why Array in JavaScript can store multiple data types.
Really helpful illustration of JS array methods
Javascript array functions cheat sheet (as asked)
Below code is an array and there two different data types are stored in. One is Integers and second is String. Is this array is data structure? If yes, why its not same data type.
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
Please explain why Array in JavaScript can store multiple data types.