Generate a map that encompasses keys using identical letters along with corresponding values

This is an item

var item = {
    abc: 'xyz', 
    a: 12, 
    cba: 'xyz2', 
    ba: 22, 
    ab: 33, 
    abcde: 44 
};

and here is the output of

console.log(flatSimilarKeys(item));
:

https://i.sstatic.net/vvMRO.png

Answer №1

To separate, organize and combine the key in a specific manner while also gathering the respective values, you can utilize the method of splitting the key, sorting it, and then joining it with an empty string. Here is an implementation to achieve that:

function flatSimilarKeys(object) {
    var result = Object.create(null);           // creating an object without prototypes

    Object.keys(object).forEach(function (k) {
        var key = k.split('').sort().join('');   // modifying the key by splitting, sorting, and joining

        result[key] = result[key] || [];
        result[key].push(object[k]);
    });
    return result;
}

console.log(flatSimilarKeys({ abc: 'xyz', a: 12, cba: 'xyz2', ba: 22, ab: 33, abcde: 44 }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Unable to generate STYLE element within iframe header

Check out this jsfiddle link: http://jsfiddle.net/uTy5j/7/embedded/result/ I've encountered an issue with CodeMirror where it seems to erase the style tag I create using the following code: var preview = document.getElementById('preview') ...

The process of extracting data from a file and converting it into dictionaries, along with peculiar observations

When attempting to create dictionaries from a file using either eval() or ast.literal_eval(), strange results are being produced and the reason behind this is unclear. The file named file.txt contains the following data: {0 : {1: 6, 1:8}, 1 : {1:11}, 2 : ...

Decomposing a Vuex module into distinct files with Nuxt: A step-by-step guide

In the official Nuxt documentation (here), it is mentioned that 'You can choose to divide a module file into separate files: state.js, actions.js, mutations.js, and getters.js'. While there are examples of breaking down the Vuex store at the roo ...

JavaScript error: Resource could not be loaded

When I have a js function called by an onclick event in a radio button, it doesn't work if the function is placed in the same ascx file where the radio button is defined. To resolve this issue, I moved the function to the ascx that includes the ascx w ...

Adjusting the height of a flexbox column to fit three rows within the space of two

Exploring the wonders of Flexbox and delving into its functionality. I have shared a Code Sandbox link showcasing my React /bootstrap code in progress... Currently, I am developing a clock component with two buttons for adjusting time (increase/decrease). ...

Creating an HTML table with collapsed borders and hidden rows/columns using the `border-collapse

I am facing a situation where I have a table with multiple lines as shown below: <table> <tr id="line1"><td>Line</td><td>1</td></tr> <tr id="line2"><td>Line</td><td>2</td></t ...

How Can You Update the State of a Parent Component from a Child Component?

My Objective In the initial component, I retrieve items with a status of 2 and display them as checkboxes. In the subsequent component, I update the status of these items to 3. In the third component, a modal opens after the status change from the secon ...

The placement of controls is not aligned properly when the validation summary is displayed at the top of the web page

Encountering an unusual issue with a custom validator function in my ASP.NET page when trying to display a validation summary at the top of the webpage using CSS styles... Here is the screen shot before clicking the submit button and making changes: Upon ...

transferring JSON information to a template in a NodeJs application

Currently, I am performing some filtering on my JSON data and storing the result in a variable called driver. The driver variable contains JSON data that I want to pass unchanged to the view. My main query is: How can I effectively send the data stored i ...

Issue: Actions should be in the form of plain objects. However, the given type is 'Promise'. To resolve this, consider incorporating middleware into your React Native store

Currently, I am facing an issue while trying to retrieve products from Firebase. Despite having redux-thunk installed to manage promises and using middleware in my store, I encountered the following error: Actions must be plain objects. The actual type d ...

Is there an h1 heading on this page?

Trying to enhance the accessibility of a website by properly setting up the headers. Running into issues with ensuring they are in the correct order. If there is code that could be applied across all pages to set h1 if it doesn't exist (promoting h2, ...

Clicking on the Form Submission Button to Trigger a Page Refresh

I am seeking advice on a solution to prevent the page from reloading when the Form Submit Button is clicked. Utilizing a jQuery click event, I have implemented AJAX to transfer form data to a PHP file for validation and database entry. The PHP script pro ...

Preventing Jquery Append from Adding Previous Elements

I am struggling to figure out how to display the star rating for each hotel individually. I have 5 hotels, each with a different star rating. Here is my Javascript code: function GetStarHotel() { var parent = $(' p.star '), imagePat ...

What could be the reason for my jQuery script not functioning properly on my web form that was inherited from a MasterPage?

In the head of my master page, I have included: <script src="Script/jquery.min.js"></script> <link href="Stylesheet/Master.css" rel="stylesheet" /> My webform (Login.aspx) inherits from this master page, and in Login.aspx I have: <a ...

What is the process for detaching and attaching click animations using the on() method?

I am encountering an issue with a recursive loop that executes a simple animation. These animations are controlled by the page load and clicking on controls .carousel_item. Click here for live sample JSFiddles for demonstration purposes Challenge: The pr ...

Express.js | Utilizing express.Router and handling route parameter inputs

I'm currently facing an issue with a small program I'm developing to practice Express.js. The problem lies in a separate router that is supposed to send a specific response based on the route. For example, when navigating to "/santiago", it shou ...

Exploring the capabilities of SVG and audio integration in web browsers

I am curious if it's possible to incorporate audio into an SVG file in a web browser. I came across this thread here, but unfortunately, I can't leave a comment =/ I tried implementing the code from this website, but it doesn't seem to work ...

What is the best way to update a div displayed in a dialog widget?

Below is the configuration I am using: <td> <div style="width:100px;height:30px; background:#009814; border:medium solid; border-color:#fff" onclick="showPopup(this)"> </div> <div style="display:none" title="Indicators">< ...

Word.js alternative for document files

I'm on the lookout for a JavaScript library that can handle Word Documents (.doc and .docx) like pdf.js. Any recommendations? UPDATE: Just discovered an intriguing library called DOCX.js, but I'm in search of something with a bit more sophistic ...

What is the method to implement an invisible filter in AngularJS?

When I enter text into the input field in a list view, it filters my list based on the visible username. For example, typing "pa" will display only one row that matches the filter. However, when I type in the "keyword filter" input field, which is not visi ...