What is causing JavaScript to pass the parameter name instead of the element?

Information: I am currently organizing an array of names into two separate arrays - one for names starting with A-M and the other for names starting with N-Z. My goal is to have each entry represented as an object with the name as the property and an empty string as the value, structured like this --> {'Name' : ' '} . However, all entries are coming out as follows --> {val: ' '}

Here is how I am pushing entries --> arrAM.push({val: ' '});

Query: How can I alter the above method to include the actual Name instead of just "val"? Thank you in advance for any assistance!

var separate = function(array){
  var arrAM = [];
  var arrNZ = [];
  _.each(array, function(val){

    if (/^[a-m]/i.test(val)){
        arrAM.push({val: ''});
    }
    else{
        arrNZ.push({val: ''})
    }
})
return arrAM;
}

Answer №1

One of the convenient features introduced in ES6 is a shortcut for using computed property names in object initializers:

arrAM.push({[val]: ''});

In versions before ES6, you had to use bracket syntax in a separate statement like this:

var obj = {};
obj[val] = '';
arrAM.push(obj);

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

Combining byte array values with VBA

This is the specific question I am referencing: PBKDF2 Excel UDF and how to concatenate INT(i) The original poster (OP) did not include the code for his "ConcatenateArrayInPlace" function, which he used in his own solution to the problem. I am attempting ...

Enhancing link functionality with jQuery on a dynamically generated server page

I am facing an issue with my navigation menu that includes dropdowns. On desktop, the parent items need to be clickable as well, which is not a problem. However, for it to be responsive on mobile devices, I need to account for the lack of hover capability. ...

What sets $vm.user apart from $vm.$data.user in Vuejs?

When you need to retrieve component data, there are two ways to do so: $vm.user and $vm.$data.user. Both methods achieve the same result in terms of setting and retrieving data. However, the question arises as to why there are two separate ways to access ...

Adjust properties based on screen size with server-side rendering compatibility

I'm currently using the alpha branch of material-ui@v5. At the moment, I have developed a custom Timeline component that functions like this: const CustomTimeline = () => { const mdDown = useMediaQuery(theme => theme.breakpoints.down("md")); ...

Creating a JavaScript/jQuery function for summing input values within an array

I am currently working on developing a function that can add elements from input fields into an array: function addPersonToDatabase(userId){ var name = $('#name').val(); var surname = $('#surname').val(); var age = $(' ...

How to prevent all boxes in jQuery from sliding up at the same time

I am encountering an issue with 36 boxes where, upon hovering over the title, the hidden text below it should slide up. However, all 36 boxes are sliding up simultaneously instead of just the one being hovered over. Below is the script I am currently using ...

angular2 ngFor is not functioning properly

I'm having an issue where I cannot get textboxes to appear whenever a user clicks a button. I am attempting to achieve this using ngFor, but for some reason, the ngFor is not iterating as expected. Even after trying to change the array reference with ...

Tips for sharing JSON data between JavaScript files

Here is the initial script setup to utilize static .json files for displaying and animating specific content. The code provided is as follows: var self = this; $.getJSON('data/post_'+ index +'.json', function(d){ self.postCa ...

Countdown Clock for Displaying Parsing Time in C#

On my aspx page, I have a submit button that triggers the parsing of ".txt" files when clicked. The parsing process generates results stored in tables and then redirects the user to another page. However, the issue at hand is that the parsing operation t ...

Iterate through JSON data and access values based on keys using a $.each loop

I have retrieved JSON data from the controller using AJAX and now I want to access this data. The data is in the form of a list of objects (array) with key-value pairs, so I am planning to use .each() function to go through all the data. The array looks li ...

How few moves are needed to rearrange an array of integers into a permutation?

You are given a sequence of numbers d[0] , d[1], d[2] , d[3] ,..,d[n]. Each move allows you to increase any d[i] by 1, 2, or 5 where i ranges from 0 to n. Find the minimum number of moves required to transform the sequence into a permutation of [1,2,3,..,n ...

The error message states: "It is not possible to destructure the property 'createComponentInstance' of 'Vue.ssrUtils' as it is undefined for nuxt and jest."

I have been working on integrating the jest testing framework into my nuxt project, but I am facing a major obstacle. I am struggling to test a simple component and haven't been able to find a solution yet. If anyone has encountered the same issue, co ...

Can a model be generated using Angular view values?

I am facing a challenge with a form that has complex functionality such as drag-and-drop, deleting groups of elements, and adding groups of elements. I want to leverage Angular for this task. The form is already rendered with original values set. <form ...

Troubleshooting Problem: Difficulty accessing Controller in AngularJS Module

I am facing difficulties with communication between my application and a module that I have developed. Below is the AngularJS module that I created. (function (document, window) { 'use strict'; var piCart = angular.module('piCart& ...

What is the best way to retrieve a single field in a MongoDB query?

Here is some sample data: { id : 1, book: "Flash", chapters: [ { chap_no: "1", sub_chapter: [ {sub_no: 1, description: "<description>" }, {s ...

What is the best way to reveal the following div while concealing the one before it?

Just starting out with Javascript, I'm currently developing a quiz solution that utilizes divs and buttons to navigate through the questions. I've written some JavaScript code for this functionality but I'm encountering an issue where it doe ...

Unable to assign Angular 2 service data to a variable within the constructor

I am facing an issue in my Angular 2 application where I need to assign the data returned from a service function to a public variable and display it in the HTML view. While the console log shows that the data is successfully fetched, it does not seem to b ...

Utilizing ReactJS to retrieve configuration settings from a YAML file, similar to how it is done

Our team is currently using a full-stack multi-microservice application where the backend java components utilize the spring @value annotation to fetch configuration values from a yml file. This method has been effective and even the Java side of our UI c ...

Encountered an undefined error while trying to read promises

I'm attempting to receive a response from a function in order to trigger another function, but I am not receiving the expected response. I encountered the following error message: "TypeError: Cannot read property 'then' of undefined." In my ...

Transferring a PHP array to JavaScript using AJAX

I have spent time searching for answers to my issue with no success. My PHP file includes the following array: $data = ['logged' => $_SESSION['loggedin'], 'sessName' => $_SESSION['name']]; echo json_encode($dat ...