admin管理员组

文章数量:1415139

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.

Share Improve this question edited Jul 5, 2016 at 19:33 SakthiSureshAnand asked Jul 5, 2016 at 19:25 SakthiSureshAnandSakthiSureshAnand 1,3621 gold badge19 silver badges35 bronze badges 10
  • 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 this dataId? 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 , if string i am getting the length of string value which is inside the stepData . But below for loop executed that much time called , if the value is string. 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 is string . Its excuted more times created more number of data inside Database, – SakthiSureshAnand Commented Jul 5, 2016 at 19:40
 |  Show 5 more ments

4 Answers 4

Reset to default 1

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[]

From your ment on the question:

Inside for I am iterating stepData.length . So If its string only one time I should iterate ,If its string[] 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 strings 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 {
...
}

本文标签: javascriptTypescript stringstring find the array lengthStack Overflow