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
JavaScript arrays arent actually arrays at all?
Why Javascript returns "Array" Data structure as Object. Please explain why Array in JavaScript can store multiple data types.
Is it possible to create an array union types as values?
If you want to enumerate over a union type, you're best off starting with the array and creating the type from that.
This will work, for instance:
// define the array first, note the const const FruitValues = ["apple", "banana"] as const; // this magic incantation will create a union from that array type Fruits = (typeof FruitValues)[number]; const fruitBasket: Fruits[] = []; FruitValues.forEach(Fruit => fruitBasket.push(Fruit)); console.log(fruitBasket);More on reddit.com
So I have been learning computer science in college and getting specialized in web development just so I can get a better chance of landing an entry level job and I ran across something that I have been confused about. So in my understanding from my CS courses, an array is a contiguous composite data structure which holds homogeneous values which are ordered with an index. However in JS, arrays are composite data structures which hold heterogeneous values and are ordered with an index. Would an array in JS be closer to a record as far as data structures go or am I putting the cart before the horse in the importance of the allowance of more than one data structure? Is it more important that arrays are index-based by their definition more than it is important that they are homogeneous?
Any and all help would be great, thanks!!
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.