divide various arrays into separate variables depending on their names

Exploring a more efficient approach - I have these arrays at my disposal:

   var model1 = ['10', '20', '30', '40','50','60'];
   var model2 = ['80', '100', '200', '300','400','500'];
   var model3 = ['1', '2', '3', '4','5','6'];

In my code where I utilize them, I currently do the following:

    $scope.sli['model1'][0]=0;
    $scope.sli['model1'][1]=10;
    $scope.sli['model1'][2]=20;
    $scope.sli['model1'][3]=30;
    $scope.sli['model1'][4]=40;
    $scope.sli['model1'][5]=50;
    $scope.sli['model1'][6]=60;

I'm seeking a more streamlined method using a for loop. Ideally, I'd like to pass just the model array name and automatically split the array into indices. This way, any new models added in the future will be seamlessly incorporated without having to declare each one individually.

Answer №1

To avoid assigning an array, follow these steps:

Assuming that model1 is already defined as:

var model1 = ['10', '20', '30', '40','50','60'];

You can simply use the code snippet below:

$scope.sli['model1'] = model1

This way, you can access individual elements like $scope.sli['model1'][0] to retrieve "10"

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

Can you explain the distinction between the terms "vite" and "vite preview"?

I recently created a project template with the help of vite. While looking through my package.json file, I noticed the following section; "scripts": { "dev": "vite", "build": "vue-tsc --noEmit &&a ...

Encountering a 404 error on package.json when utilizing SystemJS with Karma

Issue: I'm facing challenges while trying to incorporate SystemJS config with Karma using the karma-systemjs plugin. Karma is throwing errors indicating that it cannot locate the package.json file for each import: ... 10 07 2017 13:38:15.107:WARN [ ...

I am curious to know if there is a method to obtain the pixel dimensions of a DOM element's bounding box?

Is there a method to obtain the bounding box coordinates of a DOM element using JavaScript? I could theoretically derive it from various CSS properties like width, height, and others. The reason for my inquiry is that some other graphical platforms offer ...

The only numbers that can be typed using Typescript are odd numbers

I am looking for a way to create a type in Typescript that can check if a value is an odd number. I have searched for solutions, but all I find are hardcoded options like odds: 1 | 3 | 5 | 7 | 9. Is there a dynamic approach to achieve this using only Types ...

Adjust the color of the sidebar's list items when scrolling

How can I change the background color of left sticky sidebars li on scrolling? When scrolling through BMW, BMW's background color in the sidebar should turn green. Please refer to the code snippet provided below. The background color of the li ...

Tips for implementing real-time filtering using React Native Realm technology

As I transition to Realm for React Native, I am eager to take advantage of their built-in queries and filtering capabilities. Currently, I have checkbox filters with three options, and the selected options are stored in an array: var filters = ['comp ...

Different ways to transfer variables dynamically between functions

As a beginner in jQuery, I am taking a hands-on approach to learning it. While working on manipulating a script, I encountered the following issue and could use some guidance. The script consists of a jQuery plugin with two files: one for the plugin itsel ...

Vue Application experiences issues resizing Three.js Animation in Canvas

Currently, I am deep diving into learning Three.js within a Vue.js component. Take a look at my code snippet: <script> import * as THREE from "three"; export default { name: "Scene", mounted() { this.initializeThree(); ...

Invoking function through click event binding

Check out this fiddle I have: http://jsfiddle.net/jj258ykp/2/ On BTN2 and BTN3, I get an alert when I click, but not on BTN1. I want to see the alert on BTN1 as well. Take a look at the coffeescript code snippet below: class A method: -> ...

Using Selenium WebDriver to retrieve the row number that is visible within a table

As I write a test script using Selenium WebDriver and Java for a page containing a table with multiple rows and columns, I face the challenge of dealing with hidden rows alongside displayed ones. With around 1800 total rows, only seven are visible on the ...

Obtain the user's email using nodemailer

I created a contact form using Nodemailer that I am having trouble with. Take a look at the code below: let mailOptions = { from: '<<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3636360e363636602d2123">[emai ...

Unable to concatenate an array of strings using the concat method

I'm attempting to store HTML tags and a list as a string in a JavaScript variable so that I can later replace that code within an HTML document. app.use('/index', function(req, res) { var query = req.query; var searchQuery = query.searc ...

Having trouble with PHP Storm and Vue component not working? Or encountering issues with unresolved function or method in your

I have been struggling with this issue for days and cannot seem to find a solution online. Being new to Vue.js, I am currently working on a TDD Laravel project: My goal is to add the standard Vue example-component to my app.blade.php as follows: app.bla ...

How can we detect line breaks within a selection using JavaScript?

Apologies for the lack of expertise in the content below, as it is being produced by a designer experimenting with coding :D The goal here is to determine the number of lines selected or highlighted by the cursor. When I mention "lines," I mean what is vi ...

The infinite loading feature in Vue JS does not function properly when used within a v-else directive

Hey there, I'm trying to implement a Vue infinite loading feature in my template under certain conditions but for some reason it's not working. <generic-button v-if="!viewMore" inline no-arrow @click="viewMore = true" ...

Testing the scope of an Angular directive

Encountering an issue with the unit test In my code, I have: describe('test controller', function () { var =$compile, scope, rootScope; beforeEach(module('myApp')); beforeEach(inject(function (_$compile_, _$rootScope_) { ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Struggling to retrieve JSON object sent to PHP using POST

I apologize if this question has already been asked. I have searched for a solution but nothing has worked for me yet. I am attempting to send a large JSON object to a PHP file using the POST method. Below is my JavaScript code : var grid = { ...

Showing elements in the queue array

I'm struggling to show the elements in the queue array. I can see the details of the queue array, but not the actual elements themselves. How do I go about printing the elements in the queue array? Appreciate any help in advance! public class Queue ...

How can you eliminate an element from an array of JSON by using another array for reference?

idArray= [ 'cb53f7a1-107c-44fd-9df6-adeddaa5d3f0', '428a1951-7519-421b-90ea-704002eb31dc'] issues=[ { description: 'Necessitatibus unde ', severity: 'Minor', status: 'Open', createdDate: &ap ...