Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6127127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:28:00+00:00 2026-05-23T16:28:00+00:00

I want to return an array when one of the elements matches an item

  • 0

I want to return an array when one of the elements matches an item within an array.

Is the below code the fastest way to loop through an array when a value matches in a javascript array of arrays?

Note : Welcome any suggestions to modify the variable relatedVideosArray to make it a different data structure for better performance.

var relatedVideosArray = [

["1047694110001"],
["1047694111001", "1019385098001","1020367665001","1020367662001", "1019385097001", "1020367667001"],
["1040885813001"],
["1019385094001", "1019385096001"], 
["952541791001", "952544511001", "952544512001", "952544508001", "952541790001","952580933001", "952580934001", "1051906367001"]                                        

]


function getRelatedVideos(videoClicked){

    var tempStoreArray = [];    
    var getCurrentId = videoClicked;    
    var relVideoslen = relatedVideosArray.length;

    for(var i in relatedVideosArray) {
        tempStoreArray = relatedVideosArray[i]; 
      for(var j in tempStoreArray){             
                if(tempStoreArray[j] == getCurrentId){                  
        return relatedVideosArray[i];                   
        }               
      }
    }       
}

Update: I initially thought of making a key of video ids and values as all the related ids, but I want to display the key as well as all the related ids if any of the ids within the value array are clicked. Hope this helps to explain the constraint I have.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:28:01+00:00Added an answer on May 23, 2026 at 4:28 pm

    Modern day browsers support Array indexOf.

    For the people saying the array indexOf is slower, basic tests on speed.

    var values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
    
    console.time("for");
    for(var i=0;i<1000;i++){
      for(var j=0;j<=values.length;j++){
        if(values[j]===20) break;
      }
    }    
    console.timeEnd("for");
    
    console.time("reverse for");
    for(i=0;i<1000;i++){
      for(var j=values.length-1;j>=0;j--){
        if(values[j]===1) break;
      }
    }
    console.timeEnd("reverse for");
    
    
    console.time("while");
    for(i=0;i<1000;i++){
      var j=0;
      while (j<values.length){
        if(values[j]===20) break;
        j++;
      }
    }
    console.timeEnd("while");
    
    
    console.time("reverse while");
    for(i=0;i<1000;i++){
      var j=values.length-1;
      while (j>=0){
        if(values[j]===1) break;
        j--;
      }
    }
    console.timeEnd("reverse while");
    
    
    console.time("indexOf");
    for(var i=0;i<1000;i++){
      var x = values.indexOf(20);
    }
    console.timeEnd("indexOf");
    
    console.time("toString reg exp");
    for(var i=0;i<1000;i++){
      var x = (/(,|^)20(,|$)/).test(values.toString);
    }
    console.timeEnd("toString reg exp");
    

    Two possible solutions:

    var relatedVideosArray = [
    
    ["1047694110001"],
    ["1047694111001", "1019385098001","1020367665001","1020367662001", "1019385097001", "1020367667001"],
    ["1040885813001"],
    ["1019385094001", "1019385096001"], 
    ["952541791001", "952544511001", "952544512001", "952544508001", "952541790001","952580933001", "952580934001", "1051906367001"]                                        
    
    ]
    
    //var getCurrentId = "1019385098001";
    var getCurrentId = "1040885813001";
    
    
    console.time("indexOf");
    var tempStoreArray = [];
    for(var i = relatedVideosArray.length-1; i>=0; i--){
         var subArr = relatedVideosArray[i];
         if(subArr.indexOf(getCurrentId)!==-1){
            tempStoreArray.push(subArr);
         }
    }
    console.timeEnd("indexOf");
    console.log(tempStoreArray);
    
    
    
    
    console.time("toString reg exp");
    var tempStoreArray = [];
    var re = new RegExp("(,|^)" + getCurrentId + "(,|$)");
    for(var i = relatedVideosArray.length-1; i>=0; i--){
         var subArr = relatedVideosArray[i];
         if(re.test(subArr.toString())){
            tempStoreArray.push(subArr);
         }
    }
    console.timeEnd("toString reg exp");
    console.log(tempStoreArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return a different value in string context and numeric context like
I have two arrays and want to remove from one all elements which exist
Is there an easy way to find a smaller cell array of strings within
How would one return a class computed CSS property/property array? Like, if I have
I want a simple function that receives a string and returns an array of
I want to return StudentId to use elsewhere outside of the scope of the
I want to return all application dates for the current month and for the
I want to return a private field from a remoted object but i get
I want to return a set of values from function till the point they
I want to return some errors to my jquery method. What is happening is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.