Remove the JSON key from all array elements while preserving their child elements

I have an array of JSON objects that looks like this:

var array_json = [{"my":{"id":144,"price":12500000}},
{"my":{"id":145,"price":13500000}},{"my":{"id":146,"price":13450000}},
{"my":{"id":147,"price":11500000}},{"my":{"id":148,"price":15560000}}]

My goal is to remove the "my" key from each object while keeping the other information intact. Essentially, I want it to look like this:

array_json = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]

Is there a way to achieve this?

Thank you.

Answer №1

If you want to manipulate an array in JavaScript, you can utilize the map method:

var array_json = [{"my":{"id":144,"price":12500000}},
  {"my":{"id":145,"price":13500000}},{"my":{"id":146,"price":13450000}},
  {"my":{"id":147,"price":11500000}},{"my":{"id":148,"price":15560000}}];
    
array_json = array_json.map( function( value ) {
  return value.my;
});
    
console.log( array_json )

Answer №2

JavaScript

This code snippet demonstrates how to iterate through an array and update each index with the value of a specific property.

for (let i = 0; i < array_json.length; i++) {
    array_json[i] = array_json[i].my;
}

Answer №3

Here is a code snippet that you can use:

    var array_json = [
        {"my":{"id":144,"price":12500000}},
        {"my":{"id":145,"price":13500000}},
        {"my":{"id":146,"price":13450000}},
        {"my":{"id":147,"price":11500000}},
        {"my":{"id":148,"price":15560000}}
    ];
    var objs= new Object(); 
   for (x in array_json) {
       objs[x]=array_json[x].my;
       alert(objs[x].id);
       alert(objs[x].price);
}

After running the code, the result will be stored in the 'objs' variable as shown below:

objs=[
      {"id":144,"price":12500000}},
      {"id":145,"price":13500000}},
      {"id":146,"price":13450000}},
      {"id":147,"price":11500000}},
      {"id":148,"price":15560000}}
]

You can test the code on this JSFiddle link.

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

Using Angular 2 to Create Nested ngFor Loops

I'm facing an issue that I can't seem to resolve. I attempted to use *ngFor in my code, but it seems that I am making a mistake with nested ngFor loops. To illustrate what I need to achieve, I ended up writing some messy code just to show the str ...

Creating a personalized grid display

I've been trying to achieve the following output with my code, but so far nothing has worked. Can someone help me identify what I may be doing wrong? I'm new to this, so any guidance would be greatly appreciated. (Please ignore the 'undefine ...

Execute a VueJS API call every 20 minutes

I am retrieving data from an API endpoint to display information about coin names. I would like this information to update every 20 minutes, but for testing purposes, I have set it to refresh every 500 milliseconds. However, my current approach of fetching ...

Performing numpy's fast Fourier transform on an array of numpy arrays in Python

Looking for an efficient way to perform np.fft.fftn on an array of 2D numpy arrays? Consider the following scenario: V0 is an array with a shape of (nvar, nx, ny), and you want to carry out FFT on each 2D array along the first dimension of V0. In the code ...

Is it possible to examine my API request URL from the API's perspective using a Linux Command Line Utility?

Curious to uncover the request URL I'm sending to a JSON API, as I suspect my URL parameters might be getting removed by a proxy server. How can I verify this information? Is there a method to view the request URI using tools like cURL, WGET, or any o ...

The encoding of Node.js using npm

Looking to install the validate .json file below using npm: { "name": "node-todo", "version": "0.0.0", "description": "Simple todo application", "main": "server.js", "dependencies": { "express": "~3.4.4", "mongoose": "~ ...

How can I integrate custom PHP pages into Odoo Community 12?

I am looking for a way to integrate my custom PHP webpages with the login page of Odoo Community that is already set up and functioning on my server. I want specific users to be redirected to my custom pages after logging in. Any suggestions on how to ac ...

The click event for jQuery is failing to trigger on every link

Currently, I'm in the process of creating a "collapse all" button for the Bootstrap 3 collapsible plugin. It appears to be functioning correctly, but only when there is just one collapsible on the page. Once I add another one, the method only works on ...

Chrome now supports clickable circular canvas corners

I have a unique setup with two canvases. I've customized them to be circular by utilizing the border-radius property. The second canvas is positioned perfectly within the boundaries of the first one using absolute positioning. This is where it gets e ...

The directional rotation plugins of GSAP are incompatible with the plugins of PIXI

I've been experimenting with using directional rotation plugins in combination with pixi.js plugins. Unfortunately, I'm encountering some issues. If you check out the codepen link: https://codepen.io/asiankingofwhales/pen/RyNKBR?editors=0010, yo ...

Regular expression to match a string with a minimum of 10 characters, including at least 3 digits and alphanumeric

I need assistance creating a regular expression that validates strings with a minimum of 10 alphanumeric characters and at least 3 digits. For example: X6JV2YUW8T => True JN1H86LEKA => True 111JEJE134 => True A111111111 => True ABCDEFGHIJ =&g ...

What steps should be taken to trigger an API call once 3 characters have been entered into a field

In my current project, I am dealing with a parent and child component setup. The child component includes an input field that will emit the user-entered value to the parent component using the following syntax: <parent-component (sendInputValue)="g ...

Is there a way for me to access the information within these curly brackets [[]}?

I'm facing a challenge where I need to extract the ID from an API response that is formatted in a way unfamiliar to me. As a result, I'm unsure of how to retrieve the ID data from this response. (This is my initial query, so if it's unclear ...

Error importing reach-router in Gatsbyjs causing website to break

While working on my Gatsby project, I decided to incorporate the React Cookie Consent package. However, upon installation and implementation attempt, my website crashed, displaying this error message: warn ./.cache/root.js Attempted import error: &a ...

Is it possible to apply capitalization or convert the value of props to uppercase in React?

Despite attempting the toUpperCase method, I am unable to capitalize my props value. Here's the code I have: export default function Navbar(props) { return ( <> <div> <nav class="navbar navbar-expand-lg bg-b ...

Serialize the elements within an array using JSON.stringify

I'm trying to convert an object into a string, but for some reason it's not working correctly: function httpRequest(url) { this.url = url; this.headers = []; } var req = new httpRequest("http://test.com"); req.headers["cookie"] = "version=1 ...

Discovering the window scroll unit when clicking, as a page smoothly transitions to the top for a specified target position

Is there a way to achieve parallax scrolling on window scroll and navigation click simultaneously? When navigating and animating the page to the top to show the target, I also want to get the window scroll unit. How can I obtain the window scroll unit on ...

Saving the information into a designated MongoDB repository with a particular title

One of my recent projects involved creating a script that pulls data from the Binance API and sends it to MongoDB. This script runs every hour using the Node-Schedule package, fetching three different sets of data based on their symbols (BTCUSDT, ETHUSDT, ...

Is It Possible to Extract a Hidden Document Value from a Web Browser Using IWebBrowser2 in LabVIEW?

After hours of combing through the internet, I've come up empty-handed in my quest to find information related to my current project. I've created an HTML document that gathers user data and stores it in a JavaScript array. This array is then com ...

What is the best way to transfer a CSV file to my local JSON server using React?

I have a CSV file that needs to be converted and uploaded to my local JSON server. After attempting to achieve this using papaparse library, I wrote the following code: const uploadData = () => { for (let i = 0; i < data.length; i++) { let ...