What is the best way to structure a data object in Javascript or VueJs?

As I work on developing a small application using VueJs, the data I receive is structured in a particular format:

{
    "interactions":[
        {
            "id":14,
            "user_id":1,
            "schedule":"2017-06-04 05:02:12",
            "type":"Meeting",
            "with_client":0,
            "event_type":"2",
            "venue":"Mumbai",
            "created_at":"2017-06-04 07:15:37",
            "updated_at":"2017-06-04 07:15:37",
            "deleted_at":null,
            "meeting":{
                ...
            }
        },
    ]
}

The data contains much more detail which I aim to structure like this:

forEach(client_association)
{
    ...
}

forEach(stellar_participants)
{
    ...
}

forEach(contacts_participants)
{
    ...
}

Ultimately, I want the final format to look something like this:

meeting_date = 2017-06-04 05:02:12 //Schedule
meeting_call = Meeting //type
event_type = 2 //event_type
venue = Mumbai //venue
with_client = 0
stellar_participants = Analyst //stellarParticipants
clients_association = Check 2 Contact, Ammy Contact //adding all the names in clients association
contacts_association = Check Contact, Check 1 Contact //adding all the names in contacts association

This consolidated format will make it easier for me to filter the data effectively. Any guidance on achieving this would be greatly appreciated. Thank you.

Answer №1

Here is a possible solution: simply use the map function to iterate through your data. This code generates an array where each element is an object representing an item in the interactions array:

var a = {
    "interactions":[
        {
            "id":14,
            "user_id":1,
            "schedule":"2017-06-04 05:02:12",
            "type":"Meeting",
            "with_client":0,
            "event_type":"2",
            "venue":"Mumbai",
            "created_at":"2017-06-04 07:15:37",
            "updated_at":"2017-06-04 07:15:37",
            "deleted_at":null,
            "meeting":{
                "id":14,
                "user_id":1,
                "schedule":"2017-06-04 05:02:12",
                "type":"Meeting",
                "with_client":0,
                "event_type":"2",
                "venue":"Mumbai",
                "created_at":"2017-06-04 07:15:37",
                "updated_at":"2017-06-04 07:15:37",
                "deleted_at":null,
                "clients_association":[
                    {
                        "id":4,
                        "company_id":8,
                        "salutation":"Mr",
                        "first_name":"Check 2",
                        "last_name":"Contact",
                        "number":"098765",
                        "email":"example1@example.com",
                        "alt_email":null,
                        "address":null,
                        "city":null,
                        "state":null,
                        "country":null,
                        "profile":"Investor-Senior",
                        // More data...
                    },
                    // More associations...
                ],
                // More interactions...
            }
        },
    ]
};

var res = a.interactions.map(i => Object.assign({
  'meeting_date': i.schedule,
  'meeting_call': i.type,
  'event_type': i.event_type,
  'venue': i.venue,
  'with_client': i.with_client
  }, {
  'stellar_participants': i.meeting.stellar_participants.map(sp => sp.name).join(', ')
  }, {
  'clients_association': i.meeting.clients_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ')
  }, {
  'contacts_association': i.meeting.contacts_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ')
  }));

console.log(res)

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

What could be causing the remaining part of the template to not render when using an Angular directive?

After adding my custom directive to a template on an existing page, I noticed that only the directive was rendering and the rest of the template was not showing up as expected. Even though the controller seemed to have executed based on console logs and B ...

Tips for creating text that adjusts to the size of a div element

I'm currently working on developing my e-commerce website. Each product is showcased in its own separate div, but I've encountered a challenge. The issue arises when the product description exceeds the limits of the div, causing it to overflow ou ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

Renaming errors within a project with a complex nested structure using npm

I am encountering an issue in my NodeJS project which consists of nested subprojects with their own package.json files. Whenever I make changes to dependencies in the subprojects, I encounter errors similar to the one below: npm ERR! code ENOENT npm ERR! ...

The JavaScript code that functions properly in Codepen is not functioning on my HTML page

Can you help me understand why this code is working fine in CodePen, but not on my HTML page? I'm unable to identify the error that may have occurred. Any assistance would be greatly appreciated! I suspect there's an issue with connecting some jQ ...

Remove objects from an array if they share identical key values

I'm having trouble getting this to work. I have an array of objects that looks like this: let myCities = [ { value: 'Barcelona', code: 02342837492482347 }, { value: 'Rome', code: 28282716171819 }, { v ...

Using SimplyJS and XML: extracting the value of a specific key

Hey there, I wanted to update you on my progress with the Pebble Watch project. I've switched over to using an official external API to make HTTP requests for values, and now I'm receiving results in XML format instead of JSON. Here's a ...

What is the reason for HereMap factoring in stopOver time when calculating travel time for the destination waypoint?

I am currently working on a request using the HereMap Calculate Route API. Waypoint 0 does not have any stopOver time, but waypoints 1 and 2 do have stopOver times. Below is an example of the request: https://route.ls.hereapi.com/routing/7.2/calculateroute ...

The promise catch method does not handle JSON parsing correctly

Utilizing Angular's Http to interact with my API has been successful for handling responses with a status of 200. The data is parsed correctly and outputted as expected within the first .then() block. However, when encountering an error with a status ...

Facilitating the integration of both Typescript and JavaScript within a Node application

We are currently integrating Typescript into an existing node project written in JS to facilitate ongoing refactoring efforts. To enable Typescript, I have included a tsConfig with the following configuration: { "compilerOptions": { "target": "es6", ...

Float: A threshold reached when adding 1 no longer has any effect

In the realm of programming, float serves as an estimation of a numeric value. For example, 12345678901234567890 === 12345678901234567891 evaluates to true However, 1234567890 === 1234567891 yields false Where does the equation num === num+1 reach a br ...

Unable to connect beyond the local network using Socket.io

server.js import { Server } from "socket.io"; const io = new Server(8000); io.on("connect", (socket) => { console.log(`connect ${socket.id}`); socket.on("ping", (cb) => { console.log("ping"); ...

Can we set a restriction on the maximum number of children a particular div can contain?

I'm trying to find a way to restrict the number of children in a div. The concept is that there is a selected commands div where you can drag and drop available commands (essentially buttons) from another div called available commands. However, I wan ...

Selecting elements in VueJS and jQuery using query selectors

I'm currently facing an issue with accessing a DOM element inside a .vue component. It seems like the element hasn't rendered yet. One workaround that I found is using a setTimeout function: setTimeout(() => { $('.element').do_ ...

The route is redirecting to an incorrect destination

Whenever a button is clicked on my webpage, this particular function is triggered. $scope.go=function(takenAt){ var path = '/oneMinuteMetric/loadCapturedMetrics?'+'&timestamp=' + takenAt + '&tagName='+ $stateParam ...

Guide to transmitting and managing a JSON document utilizing JavaScript

When working on the server side, I receive a simple JSON file via REST that contains various IDs. Here is an example: [ { "_id": "5825a49dasdasdasd8417c1b6d5", } "_id": "dfsdfsdf4960932218417c1b6d5", } "_id": "23434344960932218417c1b6d5", },] To handle t ...

Material UI TreeView: Organize and present node data with multiple columns in a tree structure

const treeItems = [ { id: 1, name: 'English', country: 'US', children: [ { id: 4, name: 'Spring', country: 'Uk', ...

ReactJs allows for fluid scroll animations that persist as long as the mouse is clicked or a button

Just some background information: I'm aiming to replicate the scrolling effect seen in Etsy's product image thumbnails carousel. Essentially, when you hover over the top part of the div, it automatically scrolls down until the last image is reve ...

Identifying if a variable is redirecting

Dealing with React Router Dom V6 I am facing an issue with two loader functions that make server requests. async function fetchUserDetails(userId, userAction) { const token = getAuthToken(); const userData = await axios({ url: API.endpoint + &apos ...

The shared module for next/router is not found in the shared scope default within Next.js and @module-federation/nextjs-mf

I am working on a JavaScript Turbo repo with a host app that has the following configuration in its next.config.js: const { NextFederationPlugin } = require("@module-federation/nextjs-mf"); const nextConfig = { reactStrictMode: true, ...