• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

118
Views
What is javascript/typescript equivalent of this python function?

I couldn't convert this syntax to js or ts:

    def get_index(self):
        path = self.get_request_path()
        if "/_async_search" in path and "/_search":
            # no index we wildcard
            return next((sub_part for sub_part in path.split("/") if sub_part != ""), "*")
        return None
almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think the main complication to do the javascript translation of your python code is to lazy load the list returned by path.split("/") like it's done in your python code.

For that, you can use a generator function like the following:

function* first_substring(path) {
  for(substring of path.split("/")){
    if(substring !== ""){
        yield substring;
    }
    else{
        yield "*";
    }
  }
}

Then your javascript translation would be something like:

function get_index(){
    const path = this.get_request_path();

    if(path.indexOf("/_async_search") !== -1 && "/_search"){
        return first_substring(path).next().value;
    }

    return None

}
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error