Iterate through each element in all arrays, but limit the loop to only iterate through the elements in the first array up to its

Within my collection of arrays, each containing elements, the structure is as follows:

var result = [
// starting with the outer array
    [
    // initial internal array
        {name: 'A', value: 111},
        {name: 'B', value: 222},
        {name: 'C', value: 333},
        ...
    ],
    [
    // following internal arrays #2
        {name: 'D', value: 55},
        {name: 'E', value: 99},
        ...
    ],
    // more internal arrays #3
        {name: 'F', value: 1000},
        ...
    ],
    ...
    ...
]

My objective is to iterate through each element (A-F), but limit the looping to only the elements within the first array (A-C).

The desired output is:

AA, AB, AC, AD, AE, AF

BB, BC, BD, BE, BF

CC, CD, CE, CF

UPDATE: Since I do not have the exact lengths of any of the arrays, fixed values cannot be used.

Additionally, the example above is not restricted to just 2 arrays.

Answer №1

// Merging multiple arrays into one
var mergedArray = result.reduce(function(mergedArray, currentArray) {
    return mergedArray.concat(currentArray);
}, []);

// Iterating through the first array
for (var i = 0; i < mergedArray[0].length; i += 1) {
    // Starting from current index and iterating till the end
    for(var j = i; j < mergedArray.length; j += 1) {
        console.log(mergedArray[i].name + mergedArray[j].name);
    }
}

Desired Output

AA
AB
AC
AD
AE
AF
BB
BC
BD
BE
BF
CC
CD
CE
CF
...
...

Answer №2

I admire the inventive approach proposed by @thefourtheye, however, it overlooks a crucial aspect of your query. To address this, consider implementing the following adjustment:

// Combine multiple arrays into one
var startingArray = result[0],
    allElements = result.reduce(function(result, current) {
        return result.concat(current);
    }, []), x, y;

// Loop through the array
for (x = 0; x < startingArray.length; x += 1) {
    // Begin at the current x position and loop till the end
    for(y = x; y < allElements.length; y += 1) {
        console.log(startingArray[x].name + allElements[y].name);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Send a redirect after a certain delay to a URL stored in a variable outside of the current scope

Upon making an ajax request, the JSON response contains a link that needs to redirect the user to another page after 3 seconds. The current approach used is: response = JSON.parse(res); var link = response.link; setTimeout("window.location.href=link",300 ...

Vue.js error: Unable to locate requested resources

Recently, I've been encountering a curious issue when trying to access a specific component in my application. When I manually type the URL http://localhost:8080/detailed-support/start into the browser, the content loads without error. However, none o ...

Add some flair to your list by animating the text within it

My goal is to add animation to the text within the list. Below is the code snippet: <div id="about" class="row section"> <div class="col-sm-8"> <h2 style="color:black;">About me!</h2> <ul> <li > ...

Parameters for MongoDB search queries

I am currently working with a MongoDB query on a specific collection. Here is the structure of the query: common.db.collection('vb.vbStats').find({uid:uid, "vbs.vbNID":vbNID}, {"vbs.$":1}).toArray(function(err, result) {....} The collection I a ...

Tips for preventing the occurrence of numerous instances of braintree.setup in Angular

I am encountering an issue with a Braintree payment form displayed in a modal window: $scope.displayModalBraintree = function () { $scope.modal = 'modal_payment_form.html', $scope.$on('$includeContentLoaded', function () { ...

What steps do I need to take to ensure that this Regex pattern only recognizes percentages?

I am attempting to create a specific scenario where I can restrict my string to three digits, followed by a dot and two optional digits after the dot. For example: 100.00 1 10.56 31.5 I've developed a regex pattern that allows me to filter out any ...

Learn how to create a web application by utilizing HTML5 and REST, and then seamlessly transfer the front-end code to a mobile app using Cordova

Can a mobile application be developed from a web portal using Cordova or similar frameworks? The web portal will consist of: HTML5 for front-end J2EE for back-end with JAX-RS Is it feasible to extract pages from the web portal and integrate them into a ...

Encountered an error in index.js while using JavaScript: ".babelrc error - please refrain from copying comments as the object could not be cloned."

Update: it appears that the issue is linked to my .babelrc file: { "presets": ["env", "react"], "plugins": ["transform-class-properties", "transform-object-rest-spread"] } Removing this f ...

Progressive rendering of a substantial mesh using three.js

How can I efficiently render a large static mesh in three.js, even if it's 2 GB with tens of millions of polygons? My plan is to stream the mesh geometry buffers into indexedDB and render them progressively to the screen, all while maintaining an int ...

Is it possible to generate HTML form fields with fixed positions when using Orbeon forms?

Is there a way in Orbeon forms to position input fields at specific coordinates, relative to a background image? I currently have a paper form that fills an entire page. My goal is to use the scanned image of this form as a background and then overlay inp ...

Modifying icons with JavaScript

When I click on the play icon, it changes to the pause icon. However, I am only able to change the first icon from play to pause in my JavaScript code. How can I apply this functionality to all the audio elements on the page? let playIcon = "https://ima ...

Learn how to showcase a predetermined option in an HTML select element using Vue.js and Minimalect

I am currently utilizing Vue.js along with the Mininmalect HTML select plugin to showcase a list of countries by their names and values, where the value represents the 2 digit country code. Successfully, I have managed to implement the plugin for selectin ...

Tips for managing asynchronous code that invokes other asynchronous code in AngularJs

My factory utilizes indexedDB and a method called getRecipe that relies on this indexed db to fetch data. The issue arises because indexedDB returns its instance in an asynchronous call, and getRecipe is another method that also returns its value asynchro ...

The Spotify Whitelisted URI may show an error message: { "error": "invalid_grant", "error_description": "Redirect URI is invalid" }

Although there are similar questions out there, I'm facing a different issue. I am trying to obtain an access token from Spotify API, but all I keep getting is the dreaded "invalid redirect uri" error. Below is my API call: const request = require(& ...

JavaScript multiplying an array in HTML

Snippet of HTML code <input name="productCode[]" value="" class="tInput" id="productCode" tabindex="1"/> </td> <input name="productDesc[]" value="" class="tInput" id="productDesc" readonly="readonly" /></td> <input name="pr ...

Creating divs that adapt to different screen sizes without relying on flexbox

Currently, I am in the process of developing a chat interface where the viewport height is set to 100vh. The layout consists of a header/navbar, a "main content" section, and a footer housing the input field (you can view the code here) The way I have str ...

Is it possible to pass a random variable into an included template in Jade?

In my jade template called 'main page', I have a reusable template named 'product template'. The 'product template' is designed to display dynamic data and needs to be flexible enough to be used in multiple pages without being ...

Subheaders that stay in place while scrolling through a table with a stationary header

My goal is to design a table with a fixed header that allows the body to scroll under the header. While this is a common design, I am facing the challenge of implementing sticky subheaders. These subheaders should scroll along with the table until they rea ...

JavaScript failed to execute

I am trying to make the JavaScript function execute when the subcat-tab class is clicked, but for some reason it's not working. There are no error messages showing up, but nothing is happening. This is within a phtml file in Magento 2. <a href=&qu ...

What is the most efficient way to calculate the sum of all the numbers lying between the endpoints

I am faced with a complex array The array looks like this: let arr = [[2, "OR", 22, 22, "OR", 22, 20], [300, "OR", 22, 300, "OR", 22, 22, "OR", 1], [1212, 2, "OR", 1]] My goal is to extract and ...