I am finding the length of string|string[] , I have variable as a data type of var stepData : string | string[]
.
Some times I am getting a single string value , also possible to get the list of string . Using the length of array I am doing some other operation.
In my code I am doing some for
loop function using that stepData.length
. Here the sample my code
const stepData: string|string[] = this.$stateParams.stepData;
// Here two possible length value I am getting
// If the stepData is string[] get length of array value
// If the stepData is string get the length of string
// The `lengthVar` depends on below for loop
if (stepData.length > 0) {
var lengthVar = stepData.length;
for (var i = 0; i < lengthVar; i++) {
// Inside im writing some ajax call
}
}
How to find the exact array length of stepData
I want array value only using I am calling AJAX method inside . If any suggest I am trying any wrong or give ur valuable suggestions please.
I am finding the length of string|string[] , I have variable as a data type of var stepData : string | string[]
.
Some times I am getting a single string value , also possible to get the list of string . Using the length of array I am doing some other operation.
In my code I am doing some for
loop function using that stepData.length
. Here the sample my code
const stepData: string|string[] = this.$stateParams.stepData;
// Here two possible length value I am getting
// If the stepData is string[] get length of array value
// If the stepData is string get the length of string
// The `lengthVar` depends on below for loop
if (stepData.length > 0) {
var lengthVar = stepData.length;
for (var i = 0; i < lengthVar; i++) {
// Inside im writing some ajax call
}
}
How to find the exact array length of stepData
I want array value only using I am calling AJAX method inside . If any suggest I am trying any wrong or give ur valuable suggestions please.
-
it's not very clear what you want. do you want to deffrintiate between the two possible types of
stepData
? if so, how? and what's thisdataId
? please explain what you want better. – Nitzan Tomer Commented Jul 5, 2016 at 19:30 - What's the matter with .length ? That does exactly what you want on both types. – AlexG Commented Jul 5, 2016 at 19:34
-
Exactly I want the
stepData
, when its string and when its string[] finding the length of array i am calling AJAX inside for loop – SakthiSureshAnand Commented Jul 5, 2016 at 19:35 -
No, If I am getting
string[]
I got diffrent length , ifstring
i am getting the length of string value which is inside thestepData
. But below for loop executed that much time called , if the value isstring
. This is the problem I am facing – SakthiSureshAnand Commented Jul 5, 2016 at 19:37 -
@AlexG Below
for
loop I am executing only length of array only , if the value isstring
. Its excuted more times created more number of data inside Database, – SakthiSureshAnand Commented Jul 5, 2016 at 19:40
4 Answers
Reset to default 1Just 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[]
From your ment on the question:
Inside
for
I am iteratingstepData.length
. So If itsstring
only one time I should iterate ,If itsstring[]
I should find the length of string array that much time I iterate
In that case, your code must be able to differentiate between string
and string[]
with a check at runtime. A simple check to differentiate between the two would be typeof stepData === "string"
, which will be true
for string
s but false
for string[]
s.
if (typeof stepData === "string") {
// here stepData is a string
} else {
// here stepData is a string[]
}
If you want to know if some variable is a string or string[] you could use the keyword instanceof:
if(stepData instanceof Array<string>) {
...
} else {
...
}
or
if(typeof stepData === "string") {
...
} else {
...
}