You can get the length of an array in JavaScript using
arrayVariable.length
Roughly like this:
arrayVariable = [1, 2, 3, 4, 5]
console.log(arrayVariable.length);
--
In your code you have not created an array. allCars is an object.
You could get the number of properties in your custom object using Object.keys():
var numberOfKeys = Object.keys(allCars).length;
If you need this, I have concerns about the underlying data model, though.
--
You could also use a map data structure instead of an object and then use the size property.
let allCars = new Map();
allCars.set('Ford, 'Ka);
allCars.set('GM', 'Sonic');
allCars.set('Audi', 'A4');
console.log(allCars.size);
But, I think this is going down a very different route than the code you already shared.
Answer from JeffryHouser on Stack OverflowYou can get the length of an array in JavaScript using
arrayVariable.length
Roughly like this:
arrayVariable = [1, 2, 3, 4, 5]
console.log(arrayVariable.length);
--
In your code you have not created an array. allCars is an object.
You could get the number of properties in your custom object using Object.keys():
var numberOfKeys = Object.keys(allCars).length;
If you need this, I have concerns about the underlying data model, though.
--
You could also use a map data structure instead of an object and then use the size property.
let allCars = new Map();
allCars.set('Ford, 'Ka);
allCars.set('GM', 'Sonic');
allCars.set('Audi', 'A4');
console.log(allCars.size);
But, I think this is going down a very different route than the code you already shared.
You need to create an array to get it's length. The syntax you have creates just an object.
class Vehiculo { // represents one vehicle
name: string;
model: string;
}
let allCars = Vehiculo[] = [
{name: 'Ford', model: 'KA'},
{name: 'Audi', model: 'A4'},
// ...so on
];
console.log(allCars.length); // will print number of cars
Typescript string | string[] find the array length
How to check array length in typescript - Stack Overflow
how to use typescript to get the length of an array of objects
Check index array length
Just do something like:
const _stepData = (typeof stepData === 'string') ? [stepData] : stepData;
then iterate it as an Array. More consistent, less bugs.
You're questions is very unclear, but I'll take a guess that you're asking how to know whether the value in stepData is a string or string[], if that the case then:
if (typeof stepData === "string") {
// stepData is a string
} else {
// stepData is a string[]
}
or
if (stepData instanceof Array) {
// stepData is a string[]
} else {
// stepData is a string
}
You can make your stepData not a const and then:
let stepData: string|string[] = this.$stateParams.stepData;
if (typeof stepData === "string") {
stepData = [stepData];
}
// now stapData is string[]
This might help with your first case: https://stackoverflow.com/a/52490977/12414867
For example:
type TupleEq<TItem, TLength extends number> = [TItem, ...TItem[]] & { length: TLength };
const a: TupleEq<number, 4> = [1, 2, 3, 4];
As for the second case, I'm not sure if it's possible in TS :/
use Object no Array
Exmaple:
type ARRAY<T,?> = {
array:Array<T>
}
» npm install typed-array-length