Guide on transforming an array containing indexed objects into a simple object

Can anyone help me with converting an array of this specific type?

place:
[
     {
         "_id": "xxxxx",
         "loc": [
             0: "xxx",
             1: "xxx"
         ]
     }
]

Into something like the following format:

place:
{
  lat: "xxx"
  lng: "xxx"
  _id: "xxxxx"
}

I've attempted using javascript, but my code below didn't yield the desired result:

let object = {};

place.forEach(element => {
     object[element[0]] = element[1];
});

I'm working in angular and need to find a way to convert the array of objects being displayed.

Answer №1

Considering the completely invalid

"location": [
    0: "xxx",
    1: "xxx"
]

is essentially just

"location": [
    "xxx",
    "xxx"
]

let place =
[
     {
         "_id": "xxxxx",
         "location": [
             "xxx",
             "xxx"
         ]
     }
]

let { _id, location: [latitude, longitude]} = place[0];
let result = { _id, latitude, longitude};
console.log(result);

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

Enhancing Connectivity Through Fastify and Fastify-HTTP-Proxy Integration

I'm currently utilizing fastify along with fastify-http-proxy on a VPS running Ubuntu 19.x that is equipped with three unique IPv4 addresses. I have confirmed the functionality of these IP addresses by testing them using the following example IPs: cur ...

Sharing JavaScript code between Maven modules can be achieved by following these steps

My Maven project has a unique structure that includes: "Base" module -- containing shared Java files -- should also include shared JavaScript files Module 1 -- using shared Java files as a Maven dependency -- should utilize shared JavaScript files throug ...

When importing a React Component with styling into the pages folder, it fails to function properly

I created a component in my components directory with custom styling: // import Link from "next/link"; import {Link} from "react-scroll" export default function Navbar() { return ( <div className="fixed w-full h-[79px] fle ...

When working with Vuejs, if you try to use "axios" in a .js file, you may encounter an error

I am currently working with VueJS and attempting to send a GET request to my API. I am using axios, but encountering an issue when trying to import it. If I use require for the import, I receive this error: Uncaught ReferenceError: require is not defined. ...

Using Vue.Js to link a value to a checkbox within a component

I'm currently developing a custom component that wraps around a checkbox (similar to what I've done with text and number input types), but I'm facing an issue with binding the passed-in value correctly. Here's the structure of my compo ...

The functionality of a generated button has been compromised

My goal is to create a webshop that doesn't rely on MySQL or any database, but instead reads items from JSON and stores them in LocalStorage. However, I've encountered an issue where the functionality of buttons gets lost after using AJAX to gene ...

Using toLocaleDateString method in Node for handling dates

After using toLocaleDateString in the browser, I noticed that it returns: n = new Date() n.toLocaleDateString() "2/10/2013" However, when using node.js, the format is completely different: n = new Date() > n.toLocaleDateString() 'Sunday, Februar ...

Fetching images from directory in xcode

I'm encountering difficulties loading images from a file into an array. I've tried different methods found online but still haven't found a solution. Being new to objective-c and a bit rusty on the rest, I could really use some guidance. In ...

Combining ReactJS event handling for onClick and onKeyDown into a single handler for TypeScript

To ensure accessibility compliance, I am incorporating onKeyPress handlers into my application. However, I am facing a challenge with interactive <div /> elements. Here are the event handlers I want to trigger on click: const handleViewInfoClick = ( ...

Having trouble reading the file using jQuery in Internet Explorer 8 and earlier versions due to its non-XML format (albeit resembling XML)

Currently, I am utilizing AJAX to load a KML file (which essentially functions as an XML file). The parsing works seamlessly in IE9, FF, and other browsers, but encounters issues in IE8. Although the data is retrieved, I face difficulties parsing it in jQu ...

"Encountering a Syntax Error When Using request.post in Dojo Framework on Button Click

Here are three questions to consider: 1) What strategies can be utilized to streamline this code and reduce nesting and quote-related complications? 2) Are there any suggestions for addressing the parsing error mentioned below? I've already tested sep ...

Only a select few expandable elements in the jQuery accordion

How can I create an accordion menu with only a few expandable options? I am looking to include the following items in my menu: Home, Support, Sales, Other The "Home" option is just a link without any sub-menu. Clicking on it should direct users to a spec ...

Tips for inserting HTML-tagged data into a database using AJAX and PHP

var id=$("#id").val(); var status=$("#status").val(); var jtitle=$("#jtitle").val(); function submitData(){ var id=$("#id").val(); var status=$("#status").val(); var jtitle=$("#jtitle").val(); var jdesc=tinyMCE.acti ...

What could be the reason for the malfunction of the loadingItem in PrimeNG's VirtualScroller

Currently, I have integrated PrimeNG 11.0.0-rc.2 with Angular 11 and am utilizing the VirtualScroller Component. However, I've encountered an issue where the loadingItem ng-template, designed to display a loader statement, is not functioning correctly ...

ngClass binding fails to update when using directives to communicate

I am looking to combine two directives in a nested structure. The 'inner directive' includes an ng-class attribute that is bound to a function taking parameters from both inner and outer scopes, and returning a Boolean value. This is the HTML co ...

Ensure that the textbox remains valid when the reset button is clicked in Bootstrap

After implementing the code for bootstrap textbox and textarea, I encountered an issue when using the reset button. When entering an invalid shop name followed by a valid address and clicking on the Reset button, it resets the form. However, if I then only ...

The export button in the React Material Table doesn't seem to be displaying correctly

[I am encountering an issue with the React export button in Material Table. The bar is not showing completely. My code includes the following: options = {{exportButton:true}} How can I resolve this?]1 ...

Enhance your image viewing experience with a React component that smoothly zooms in on images without distorting their dimensions, all with

After searching extensively for a solution, I have been unable to find one that works. My current setup involves using React with Bootstrap. I am in need of a stateless functional component that can take an image path as input and return an img element. Th ...

Vanilla JS Rock, Paper, Scissors Game - Fails to reset classes upon restarting

Many questions have been raised about this particular topic, but a solution for vanilla js seems elusive. Let's delve into a new challenge: Rock paper scissors with vanilla js. The game functions properly on its own, but the issue arises when attemp ...

Ways to display every incomplete tree contained within a singular tree

Let's start by outlining what I have reviewed and what I am specifically not interested in I am not looking to list every possible permutation from an array - Get all permutations of a PHP array? I am also not seeking to discover all combinations in ...