What is the best method for deleting JSON keys?

I'm noticing that the JSON responses I receive include redundant keys like 'entry_id', which are unnecessary. The '1':, '2':, '3':, and so on...

Is there a way to eliminate these keys from the results either during the fetch process or through manipulation afterwards?

Code for Fetch:

async function getEntries(){
    const response = await fetch(url, {
        headers: {
          Authorization: `Basic ${btoa(`${username}:${password}`)}`
        }
      })
    const json = await response.json();
    return json;
};

Here is an example Response:

{
  '1': {
    entry_id: '1',
    user_id: '0',
    date_created: '2023-09-21 16:17:10',
    value: 'text',
  },
  '2': {
    entry_id: '2',
    user_id: '0',
    date_created: '2023-09-27 15:57:43',
    value: 'text',
  },
  '3': {
    entry_id: '3',
    user_id: '0',
    date_created: '2023-09-29 20:21:33',
    value: 'text',
  }
}

Answer №1

When working with JSON, think of keys as headers instead. Take a look at the code snippet provided below to retrieve a list of objects and keys. Simply use Object.values(json). The values '1', '2', and '3' will be eliminated. Refer to the example for clarification:

const data = {
  '1': {
    entry_id: '1',
    user_id: '0',
    date_created: '2023-09-21 16:17:10',
    value: 'text',
  },
  '2': {
    entry_id: '2',
    user_id: '0',
    date_created: '2023-09-27 15:57:43',
    value: 'text',
  },
  '3': {
    entry_id: '3',
    user_id: '0',
    date_created: '2023-09-29 20:21:33',
    value: 'text',
  }
}

const dataList = Object.values(data);
//You can treat dataList as a regular object array

// Display the modified JSON data
console.log(JSON.stringify(dataList));

Output

[

{"entry_id":"1","user_id":"0","date_created":"2023-09-21 16:17:10","value":"text"},

{"entry_id":"2","user_id":"0","date_created":"2023-09-27 15:57:43","value":"text"},

{"entry_id":"3","user_id":"0","date_created":"2023-09-29 20:21:33","value":"text"}

]

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

The issue arises when attempting to use the search feature in Ionic because friend.toLowerCase is not a valid function

I keep encountering an error message that says "friend.toLowerCase" is not a function when I use Ionic's search function. The unique aspect of my program is that instead of just a list of JSON items, I have a list with 5 properties per item, such as f ...

What is the most effective method of utilizing union or extend in TypeScript when faced with a comparable scenario?

I have a function that takes in two different types of objects: const canBeGenericOrDefaultData = { id: 123, pointData: { square: 'x145', triangle: 'y145' } } function submitHandler(canBeGenericOrDefaultData: AllTheDatas | G ...

Angular JS condensed text

Is it possible to implement AngularJS in order to extract a substring from a string and add an ellipsis at the end, while also being able to ignore anchor tags within the substring? Let's consider the following example text: Lorem Ipsum has been t ...

Can you identify the nature of the argument(s) used in a styled-component?

Utilizing typescript and react in this scenario. Fetching my variable const style = 'display: inline-block;' Constructing a simple component export const GitHubIcon = () => <i className="fa-brands fa-github"></i> Enh ...

Exploring the wonders of jQuery JSON parsing

What is the best way to handle this json data using jQuery? DayEvents: [{ "0": "886", "event_id": "886", "1": "5029", "user_id": "5029", "2": "Professional", "user_type": "Professional", ... ...

Can a component be passed as props and utilized within a child Component in Vue.js?

If we have components A, B, and C in a Vue 2.0 app, A declares, registers, and uses B. Can we pass C from A to B? For example: <template> <div class="A"> <B :child_component="C" /> </div> </template ...

Tips for manipulating the size of an image directly within an Angular controller post retrieval from a database

After uploading an image to my database (with dimensions 500*700), I want to ensure that it is always displayed at a fixed size of 300*300. This requirement applies to all images, including those uploaded by users with varying dimensions. ...

Include a couple of images to the jQuery Slider

I have a website and am looking to incorporate a jQuery slider that meets my needs. However, the current slider only displays 3 pictures. How can I add one or two additional pictures to the slider? Demo: http://d.lanrentuku.com/down/js/jiaodiantu-883/ He ...

Guide on transferring datetime information from a popup dialog to an MVC controller

My goal is to create a button that, when clicked, opens a dialog allowing the selection of start and end dates. Upon clicking ok/submit, the selected datetime should be passed to a controller [HttpPost] action method. Here's what I have attempted so f ...

Issues with Javascript Date Function malfunctioning

I'm having trouble getting the text 'Oct 09, 2012' to display properly. Instead of running the function correctly, a bunch of unnecessary date text is showing up. Can anyone figure out what I'm doing wrong? Feel free to experiment with ...

Incorporating a feature that displays one show at a time and includes a sliding animation into an

Hey there! I have this show/hide script on my website that's been working well. However, there are a couple of things I need help with. I want to modify the script so that only one div can be shown at a time. When a div appears, I'd love for it ...

What is the best way to display images from JSON data in a webpage with jQuery?

I need to display the images from the JSON data in the first row, first column. What can I use to access the images before this column (data[key].description) $(document).ready(function() { if ( $('#main_intranet-main').length ) { $.getJSON(& ...

a guide to storing a TensorFlow saved model in multiple files using PHP

I have created a cutting-edge neural network model using tensorflow. I am looking to save the weights of my model every time they are updated. My initial idea was to store the weight updates in a file on the server as it learns, but the information provide ...

Oddly enough, the toggle hide/show button only activates two lines after the SECOND click, not the first

After the first click, some lines of code work properly while others only function correctly after the second click. Reviewing the comments within the code may provide a clearer understanding. I am seeking assistance in identifying where my logic is flawed ...

AngularJS triggers the function on all controllers

I encountered a problem with a specific function in my code. The issue is that the scrollFunction continues to get triggered even when I navigate to another page. I would like to limit this functionality to only apply to a particular page within the cont ...

Having an issue with the "post" and "get" functions, I am unable to make any changes to my employee table, whether it be adding

My current project involves building an app with a registration form and a login feature to access an employees table displaying their information along with the date above the table. The databases are all set up, but I am encountering issues when trying t ...

Using the hash(#) symbol in Vue 3 for routing

Even though I am using createWebHistory, my URL still contains a hash symbol like localhost/#/projects. Am I overlooking something in my code? How can I get rid of the # symbol? router const routes: Array<RouteRecordRaw> = [ { path: " ...

State of the Browser - JavaScript/jQuery

Looking for a method to identify when my browser is loading and display a loading icon. Is this approach appropriate, or should I follow a more common practice to achieve the same goal? Edit: This feature will be implemented on one of my websites during ...

sequentially animating elements using animate css in a choreographed manner

I have created a unique jsfiddle with 20 boxes that I am trying to animate using the Animate.css plugin. The plugin can be found at daneden.me/animate. My goal is to animate each box one after the other in a sequential manner, but I seem to be having trou ...

The initialization of the view keeps happening repeatedly

Currently, I'm in the process of developing a dashboard that bears some resemblance to this one Admin Dashboard Theme In my project, I am incorporating a fixed Right Side Sidebar similar to the one shown in this screenshot https://i.sstatic.net/7KdMY ...