Converting objects into arrays in JavaScript: A comprehensive guide

Given the following object :

var fix_num : 0;

kwT: {
        "0": 10.2,
        "1": 0,
      }

I am trying to figure out how to convert it into an array with this specific format :

[fix_num.key_of_object_kwT, fix_num.key_of_object_kwT, ... etc]

The desired result should look like this :

[0.0, 0.1]

Any help or guidance on this issue would be greatly appreciated.

Answer №1

Utilize Object.keys to retrieve the keys and employ map method to modify the keys.

var fix_num = 0;

var kwT = {
    "0": 10.2,
    "1": 0,
};

var result = Object.keys(kwT).map(function (key) { return fix_num + "." + key });
console.log(result);

Alternatively, you can use a basic for-in loop and store the values in a new array.

Answer №2

Personally, I prefer the previous one...

let count = 0,
    data = {
        "0": 15.7,
        "1": 5
    },
    values = [];

Object.keys(data).forEach(function (key){
    values.push(count + "." + key);
});

console.log(values); //[ '0.0', '0.1' ]

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

Sending information to ajax request upon successful completion

I needed to transfer the output of the json_encode() function to jQuery. Once successful, I intended to refresh a specific division. Action: <a id="js-delete-file" href="#" data-url="<?php echo site_url('document_items/remove/'. $value[&a ...

Having trouble executing a JavaScript function correctly when using an onclick event created within innerHTML in JavaScript

My goal is to trigger the loadInWhat function with an onclick event and populate my input field with id="what" using the value of LI function createDivBox(data){ document.getElementById("whatContainer").style.display="block"; document.get ...

Issue with Angular Route Guard - Incorrect redirection to login page

I'm encountering an issue with my Angular app where even after the JWT token has expired, I am still able to navigate within the application without any API data being accessible. I've double-checked my setup and it seems right, but for some reas ...

Conditional rendering of a component in ReactJS while maintaining the "empty" space

Is there a way to conditionally render a component without affecting the layout of other components? I'm trying to avoid unwanted shifts caused by this div https://i.sstatic.net/g1eUd.gif Do you have any suggestions? Here is a snippet of my code: ...

Tips for PHP to extract the name of the JSON array rather than its contents

Can someone assist me in making PHP read an array name instead of the content within the array? I am currently utilizing PHP to parse a JSON response received from an API call. If there is an incorrect user input, the JSON response will include: array(1) ...

JavaScript code snippet for detecting key presses of 3 specific arrow keys on the document

For this specific action, I must press and hold the left arrow key first, followed by the right arrow key, and then the up arrow key. However, it seems that the up arrow key is not being triggered as expected. It appears that there may be some limitations ...

Using JavaScript, append a variable to the existing URL

At the moment, I am using this JavaScript code snippet to load a URL based on the dropdown selection... var orderby = jQuery('#dropdown'); var str; orderby.change(function(){ str = jQuery(this).val(); window.lo ...

Tips for updating one-time binding data in AngularJS

I am currently facing an issue with a div that displays details such as mobile number, name etc. in the format: {{::mobilenumber}}, {{::name}} Within this div, there is a button that when clicked should populate the same values in a new form However, des ...

There seems to be a delay in reading data when I switch a component from stateless to stateful

Initially, I had a stateless component that was reading data perfectly on time. However, when I needed to convert it into a stateful component for my requirements, it stopped functioning as expected. The component would display empty data and wouldn't ...

Exploring methods to extract a randomized amount of JSON values from a loop in PHP

{ "success": 1, "return": { "1400151861513776": { "pair": "edc_btc", "type": "buy", "amount": 138959.22155687, "rate": 0.00000085, "timestamp_created": "1556464987", "status": 0 }, "1400151861456538": { ...

Click to remove with jQuery

My code currently wraps the label text in a span tag, but I want to remove the span tag when each span is clicked. For example, if I have test1 and test2 added, under Refined by:, clicking on test1 should remove that specific label or clicking on test2 sh ...

Steps for extracting an Ajax value from a form that contains an array value

My form input contains an array of values, from which I need to extract a value for use in Ajax and JavaScript code to populate the next form. However, my current code is not running as expected and no errors are being displayed. <table> <form& ...

How can multiple instances of a JavaScript function be executed with varying arguments?

Currently, I am using the ExtJS framework and running one method multiple times with different parameters. I am seeking a more consistent, easy, and maintainable approach to handle this. Would vanilla Javascript solutions be the way to go? I attempted to ...

Receive the Navigating event upon a new browser window opening with the help of the WebBrowser control in JavaScript

In my C# / .NET 4 project, I have a form containing a WebBrowser component that loads an external web page. An event handler is connected to the Navigating event which usually works well. However, there is an issue when a specific part of the loaded websi ...

Updating an image with a new image in Javascript or JQuery after restarting the animation cycle: a step-by-step guide

I have recently dived into the world of Javascript programming and created my very first program. Despite being a newbie for only about 3 days, I am excited to learn more and would greatly appreciate some guidance. Currently, my program is designed to func ...

Utilizing gsub with an array containing multiple hashes

I need assistance removing the spaces in the key values within the hashes output = [ {"first name"=> "george", "country"=>"Australia"}, {"second name"=> "williams", "country"=>"South Africa"}, {"first name"=> "henry", "country"=>"U ...

The JSON file I am trying to load is encountering a parsing failure over HTTP

When trying to load valid json data, I encountered the following error message: Check it out on StackBlitz! Error: Http failure during parsing for ... .json https://i.sstatic.net/hG4uQ.jpg recipe.component.ts url = '../../files/recipes.json&ap ...

Unable to get md-virtual-repeat to work within md-select?

Attempting to use md-select to showcase a large amount of data is causing the browser to freeze upon opening. To address this, I tried implementing md-virtual repeat within md-select for improved performance. However, the code doesn't seem to be funct ...

Perl - how to retrieve values from a hash containing arrays of arrays

In my perl code, I am working with a complex hash of arrays of arrays: $VAR1 = { 'A' => [ [ '1', 'PRESENT_1', 'ABSENT_2', ...

Remove items from the array that are also found in another array

I am currently working with two arrays structured as follows: this.originalArray = [{ id: 10, name: 'a', roleInfo: [{ roleID: 5, roleName: 'USER' }] }, { id: 20, name: 'b', roleInfo ...