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

Struggling with the development of a crossfading image gallery using jQuery's .animate() function while ensuring compatibility with IE8

Seeking assistance in creating a crossfading image gallery using jQuery and the .animate() function. I'm struggling to solve the issue of smooth fadeIn for the next image while maintaining compatibility with IE8. https://jsfiddle.net/Vimpil/fqhc1e9m/ ...

Jquery Query: Is it possible to incorporate variables into CSS properties?

I manage a website that supports multiple languages, and I want to adjust the position of my container based on the selected language. To achieve this, I attempted the following code: prop = lang == 'ar' ? 'right' : 'left'; $ ...

PHP - Implementing a Submit Button and Modal Pop-up AJAX across multiple browsers in PHP

As a newbie in PHP AJAX coding, I'm facing a challenge with having two browsers. My goal is to click the submit button on one browser and have a modal popup on the other browser simultaneously. Essentially creating a chat system where only a button an ...

Troubleshooting Cloud Functions: Fixing functions.logger.log not Functioning

Whenever a user updates the chat/{id} documents, this function is triggered. const functions = require("firebase-functions"); exports.onChangedChat = functions.firestore .document('chat/{id}') .onWrite((change, context) => { fun ...

What is the functionality of require(../) in node.js?

When encountering var foo=require(../), what specific modules does node.js search for? It may appear to look in the directory above the current one, but what exactly is it seeking and how does it function? Is there a comparison with include in C or impor ...

Implement an AJAX link on the webpage using Yii framework

I'm currently working on developing a user interface for an application, and I've encountered an issue with my code that involves adding AJAX links using a Yii helper function. echo CHtml::ajaxLink('Link', array('getajaxlinks&apos ...

Inaccurate formatting of body in Swift POST request received by node.js/express application

In my ios app using Swift, I am encountering an issue when sending a JSON object. The problem lies in the format of the request body on the node.js/express backend; it is awkwardly parsed where the entire JSON object from Swift is treated as the key. This ...

What is the best way to choose the checked items and calculate the total price by adding up all the prices of the selected items?

Seeking help with utilizing JavaScript to identify and collect all checked items from an HTML file. I'm looking to determine which items have been selected from a menu, each of which has an associated price. How can I access the prices of the chec ...

What is the method for displaying html files in a POST request?

This is the code snippet I am working with: app.post('/convert', function(req,res){ var auxiliar = "somo Ubuntu command line(this works)"; exec(auxiliar, function(err, stdout, stderr){ if(err){ console.log ...

What steps are involved in integrating QuickBlox on your website?

I am completely new to web development and have a question about integrating QuickBlox into my website using JavaScript. I have included the necessary JavaScript files in my website and set up the QuickBlox admin application, but I'm not sure how to p ...

Encountered an invalid prop type error while employing CSSTransition

Encountering an issue with the implementation of CSSTranstion for React. The purpose is to animate the mobile menu in a small application. Everything was functioning properly until the inclusion of the react-transition-group package, specifically using < ...

In Laravel, Inertia.js will automatically close a modal if there are no validation errors present

Here is the method I am currently using: methods: { submit() { this.$inertia.post('/projects', this.form); this.openModal = false; }, }, Unfortunately, this method closes the modal even if there are validation erro ...

Executing Actions Prior to Redirecting

I am working on implementing a timer process using a custom JQuery plugin that will redirect to the login page after a specific number of minutes. Additionally, when the timer reaches zero, I need to execute a task, which in this case involves making a cal ...

What is the reason behind the necessity of using setTimeout with a delay of at least 50ms for JS .focus()

Problem While creating a page for a client at work, I encountered an issue with a slide-out search bar. When clicking the button to open the search input field (which starts off hidden), I want the focus to shift to that input field. Oddly, I found that ...

Reading values in C# from a JSON file one after the other

Trying to extract values sequentially from the given JSON: { "FacePreset1" : { "Overall_Face_Breadth": {"x":0.35, "y":0.45}, "Upper_face_depth": {"x":0.3, "y ...

Retrieve the past week's data based on names using JavaScript

Is there a way to dynamically fetch the past seven day names, starting from today, in JavaScript? I am looking to format the result as follows: (Wednesday, Tuesday, Monday, Sunday, Saturday, Friday, Thursday, Wednesday). ...

Ways to update a component from another in React

My React code includes an Employees page that renders both a Table component and a Filter component. The Filter component manipulates some data stored in the Employees page, updates the Filter's state through useEffect, and passes the data as a prop t ...

How can I maintain the consistent speed of the Javascript timer even when the .deck is clicked?

Currently, I'm working on creating a memory card game. In this game, the deck of cards is represented by the class .deck. Strangely enough, every time I click on a card, the timer starts to speed up instead of keeping a consistent pace. How can I tack ...

Do you have to host a node server to run Angular applications?

(say) I am currently working on a project that utilizes Laravel or PHP for the back-end and Angular for the front-end. In my setup, I am using angular.js files from a CDN, which should work fine. However, I find myself confused when tutorials and books me ...

Using jQuery and AJAX to dynamically add data to POST parameters

Hello, I have a question that may sound like one from a newbie. I am trying to figure out how to insert a variable into a parameter for a POST request instead of simply writing the number in directly: var x = 3; id=(the var x needs to be here)&appid=4 ...