Change key-value pairs in an array to just values - code in JavaScript

I am working with an array that looks like the following:

[
    0:"2015",
    1:"2016",
    2:"2017",
    3:"2018"
]

My goal is to remove the keys from this array so that it appears as:

[
    "2015",
    "2016",
    "2017",
    "2018"
]

Can anyone provide guidance on how to achieve this?

Answer №1

The Object.values() function retrieves an array of an object's own enumerable property values, following the sequence of a for...in loop (except that a for-in loop also includes properties in the prototype chain).

DEMO

let x = {
    0:"2015",
    1:"2016",
    2:"2017",
    3:"2018"
}

console.log(Object.values(x))

Answer №2

Before diving into the world of programming, it's crucial to grasp the distinction between an "array" and "JSON". An array can be thought of as a collection of elements, such as [2015, 2016, 2017, 2018].

The construct you've assembled (

[0:"2015", 1:"2016", 2:"2017", 3:"2018"]
) deviates from the expected JavaScript syntax.

What you should be aiming for is a JSON object like

{0:"2015", 1:"2016", 2:"2017", 3:"2018"}
. Once you have that in place, you can utilize the Object.values() function.
const myObject = {
  0: "2015",
  1: "2016",
  2: "2017",
  3: "2018"
};
const val = Object.values(myObject);
console.log(val);

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

How to process and handle a JSON response containing multiple objects using JavaScript

I received a JSON response that looks something like this. { returnCode: 'Success', info: null, result: { payload: '{ "BatchNo":"143123", "Supplier":"Automotive", "ItemNumber":"AP123", ...

Controlling User Roles within a React JS Application

Which libraries do you rely on for managing user roles in your React projects? All suggestions and advice are appreciated. Specifically, I am interested in controlling the visibility of different application components based on the user's role. ...

When passing parameters as an array in PHP, remember to use "[ ]"

My function receives parameters as an array: public myFunction($options = array("option1"=>true, "option2"=>true, "option4"=>"astring"), $anotherparameter = 0) { if($options["option1"] === true) { //Perform all the necessary operatio ...

Issue with Iconify icon not updating when "data-icon" is set using setAttribute()

I'm having trouble trying to animate or replace an icon using the "setAttribute" method. Can someone take a look at my code and help me figure out what's wrong? <!DOCTYPE html> <html> <script src="https://code.iconify.design/1/1 ...

Invisible and Unrestricted automatic playback

Why is auto play muted in both Firefox and Chrome? How can we code it so that browsers don't block it? Here's the code I'm using: <audio id="audio1" src="https://notificationsounds.com/storage/sounds/file-sounds-1217-relax ...

The start "NaN" is not valid for the timeline in vis.js

Whenever I attempt to make a call, an error message pops up saying Error: Invalid start "NaN". I've looked everywhere online for a solution with no success. Below are the timeline options: timeline: { stack: true, start: new Date(), end: ...

The mismatch between JSON schema validation for patternProperties and properties causes confusion

Here is the JSON schema I am working with: { "title": "JSON Schema for magazine subscription", "type": "object", "properties": { "lab": { "type": "string" } }, "patternProperties": { "[A-Za-z][A-Za-z_]*[A-Za-z]": { "type" ...

Secondary Form Checkbox Input

Presently, I am working with a dynamic form-checkbox input element. <b-form-group label="Skills"> <b-form-checkbox-group v-model="form.selected" :options="options"/> </b-form-group> However, I am looking to enhance this functionalit ...

Avoid accidental overwrites in localStorage using JavaScript

I've been working on a Vue project where I'm implementing a shopping cart feature. In this setup, when the user clicks on a button, the item details are stored in localStorage and then displayed in the shopping cart interface. However, I encount ...

By simply checking off the box, I am able to remove the final entry from the database

In my HTML table, I display the rows from a specific database table. To allow users to delete certain rows, I added a column with checkboxes. However, I am facing an issue where only the last row can be deleted. For example, selecting and submitting the s ...

Steps to display a text box once the dropdown list is updated

I have a dropdown menu with two options <div class="form-group"> <span class="col-sm-4 control-span">Member Type</span> <div class="col-sm-8"> <select class="form-control" id="membertype" name="me ...

Obtain the row ID from a database by selecting checkboxes

I am facing a challenge when trying to remove a row from my MS Access database using checkboxes on my JSP page. Each row in the displayed database has a checkbox, but I am struggling to retrieve the rowId and link each checkbox with its respective row. Any ...

What is the proper method for delivering Javascript code with rendered HTTP to a client?

During the development process, I made a decision to switch to server-side rendering in order to have better control and take advantage of other benefits. My web application relies heavily on AJAX with no url redirecting, creating a website that dynamicall ...

Implement a smooth transition effect when changing the image source

I am currently working on enhancing a Squarespace website by adding a new image fade-in/fade-out effect when the mouse hovers over one of three buttons. The challenge I'm facing is that the main static image is embedded as an img element in the HTML r ...

JavaScript validation is failing to return false when re-entering the password

My JavaScript validation code is working fine. Javascript: All fields return false when re-entering the password, but JavaScript does not return false if it's not working The Re-Enter password JavaScript code is failing to work... An alert is displ ...

Implementing three identical dropdown menus using jQuery and HTML on a single webpage

It seems like I've tangled myself up in a web of confusion. I'm trying to have three identical dropdowns on a single page, each displaying clocks from different cities (so users can view multiple clocks simultaneously). However, whenever I update ...

What is the best way to swap out an icon based on the chosen option?

Here is my code: $('#mySelect').on('change', function() { var value = $(this).val(); $(".title").html(value); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href=" ...

Disable the time selection feature in the text input field

Despite seeing the same question asked before for this issue, I am looking for a solution that does not involve replacing the textbox with the same ID. When Timepicker is selected in the dropdown menu timepicker, it should activate in the textbox (which i ...

Removing the final element within a nested array: a step-by-step guide

let originalArray=[ [ "Test1", "4", "160496" ], [ "Test2", "6", "38355" ], [ "Test3", "1", "1221781" ], [ " ...

Obtain the identification of a div that was generated using jQuery

I have been struggling with two functions that don't seem to work as expected. The first function creates a div in the dom, and the second function is supposed to get the ID using getElementById(), but it's just returning null. I tried using $.D ...