0

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.

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. Commented Jul 5, 2016 at 19:30
  • What's the matter with .length ? That does exactly what you want on both types. 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 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 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, Commented Jul 5, 2016 at 19:40

4 Answers 4

1

Just do something like:

const _stepData = (typeof stepData === 'string') ? [stepData] : stepData;

then iterate it as an Array. More consistent, less bugs.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

From your comment 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[]
}

Comments

1

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 {
...
}

1 Comment

you can't use instanceof on string (or String), and checking if it's "object" doesn't get him so far as it can be a lot of things

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.