Guide to manipulating JSON Arrays of objects by utilizing key values in JavaScript

Below is the JSON data that needs to be processed using JavaScript

let inputData = [{"Test1":"121"},{"isStats":"false"},{"isKey":"true"},{"Test2":"326"}]


Desired output format

processedData = [{name: "Test1", value: "121"},{name:"isStats", value:"false"},{name:"isKey", value:"true"},{name:"Test2", value:"326"}]

Answer №1

To implement the functionality using the Array.map method, you can refer to this code snippet:

let info = [{"Name":"John"},{"isStudent":"true"},{"id":"12345"},{"Grade":"A"}];

const updatedInfo = info.map((item, i) => {
  const property = Object.keys(item)[0];
  return {
    attribute: property,
    value: item[property]
  }
});

console.log(updatedInfo);

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

Assist me in minimizing redundant code in a basic jQuery function

I successfully created a carousel using the jQuery cycle plugin with 4 links that correspond to different slides. Currently, I have separate blocks of code for each link, but I'm looking to optimize by creating a single function. $('#features-sl ...

JavaScript isn't functioning properly after UserControl is loaded via Ajax

Thank you, Stilgar for helping me solve my issue. I created a javascript file and placed all my code in it. After adding this file to the UserControl and retrieving the UserControl's html, I used $("#DivID").html(UserControlHTML). Now everything is wo ...

Exploring Multilingual Autocomplete or: Best Practices for Managing Multiple Languages in Web Applications

I'm currently developing a website and I have a mysql-table named 'items' with the following structure: item_id | item (The second column is used to identify the item_id.) In a file called language1.php, I have an array that stores the it ...

Show tweets retrieved from the Twitter API based on user input

I'm just starting out with javascript/jquery and API's are completely new to me. I could really use some assistance with a project I'm working on. My goal is to create a search function where users can enter a keyword or phrase into a textb ...

Show various columns in Select2

I am currently utilizing select2 and I am interested in displaying a multicolumn table as a dropdown. In order to achieve this, the width of the drop-down container should differ (be larger) than the input itself. Is it feasible to accomplish this? Furth ...

Is there a way to implement System Environment Variables with Angularjs Protractor?

I am looking to securely store a username and password by setting them as system environment variables and then accessing them in an AngularJS Protractor configuration file. I have defined the variables in /etc/environment. Here is what I have attempted so ...

My divs are multiplying twice as fast with every iteration of the Javascript For Loop

Recently, I developed a script that generates a series of fields based on a number provided by the user (k). Initially, I had a script that would create the correct number of fields. However, I decided to arrange them like vectors on the screen, so I made ...

Uncovering the characteristics of a GeoJSON data layer within Google Maps V3

Is there a way to access the properties of the data layer itself when loading a geoJSON file into a Google Map? I understand how to access the individual properties like posts_here, but I'm interested in obtaining the properties for the layer as a wh ...

Pass JSON data from Laravel to Controller

Recently I started using Laravel 5 and I'm encountering an issue with sending Ajax json data from the view to controller. Here is my routes.php : Route::post('ticket','TicketController@store'); Route::get('ticket', &a ...

Avoiding state transitions within Angular's ui-router from within a directive

One of the challenges I am facing involves a directive called clickable-tag, where I pass my data as the tag's name (tag.tag): <a class="item item-avatar" ui-sref="nebula.questionData({questionId: question.id})" ng-repeat="question in questi ...

AngularJS Compile directive allows you to specify functions that you want to run in

Can someone assist me in understanding how to call an external function from a built-in compile directive? Here is a code example: http://plnkr.co/edit/bPDaxn3xleR8SmnEIrEf?p=preview This is the HTML: <!DOCTYPE html> <html ng-app="app"> ...

Issues persist with utilizing the JSON put, get, and post methods within Restful webservices

I am currently attempting to send parameters to a server and retrieve the report in csv format. The code I have includes PUT, GET, and POST in that specific order. GET and POST are functioning correctly, but when I include PUT, the screen remains blank wit ...

Issue with Slick Grid not updating after Ajax request

I need to update the data on a slick grid when a checkbox is clicked, using an ajax call to retrieve new data. However, I am facing issues where the slick grid does not refresh and the checkbox loses its state upon page load. Jsp: <c:if test="${info ...

"Using Jquery to Add a New Div Element at Regular Time Int

Hey there, I could use some assistance on how to append a div element with two different speed intervals using two loops. Take a look at the code snippet below: <script type="text/javascript"> $(document).ready(function() { for (var i = 0; ...

Breaking down a lengthy series of items into several smaller lists

I have created a <ul> using the code below: function ListItem(props) { return <li>{props.value}</li>; } function ListLinks() { const listItems = footerLinks.map(section => { return section.data.map(({id, name, to}) => { ...

Getting data from a latin1 (iso-8859-1) database using javascript/nodejs: Tips and Tricks

My ancient mysql database (mysql 5.0.2) is in latin1 format and I'm encountering an issue with getting data from it. Non-ascii characters such as Â, À, and Á are appearing as 'ef bf bd' in hex, which means different characters are being d ...

Steps for updating a nested value in a json file and saving the changes:

Below is the content of a JSON file: {"Event": "ev0000001_2019", "Data": [{ "eventSummary": { "awards": [{ "awardName": "Foo", "categories": [{ "categoryName": "Best 1", ...

The type '{} is not compatible with the type 'IProps'

In my current project, I am utilizing React alongside Formik and TypeScript. The code snippet below demonstrates my usage of the withFormik Higher Order Component (HOC) in my forms: import React from 'react'; // Libraries import........ import { ...

Having trouble accessing a state variable within the map function in React

In my reactjs project, I have a class component where the following code segment is functioning properly. However, I am encountering an issue while trying to access state variables within the map function, resulting in the error message below: TypeError: ...

Execution of Javascript code does not provide the expected output when run via VS Code

I've attempted numerous times, but the desired output doesn't appear when I run it through VS Code. However, this code runs smoothly and produces the desired output when executed in Replit's online code editor. Can anyone offer assistance? l ...