How to Retrieve Values from Dynamic Arrays in JavaScript Without Specifying Keys

var data = [
    { 'Specials 5': 2192, 'dates': '2021-06-14' },
    { 'Specials 8': 767, 'dates': '2021-06-16' },
    { 'Specials 13': 2264,'dates': '2021-06-18' },
];

The Challenge is that Specials are constantly changing keys that only access the dates key-value pair. I would like the output to be as follows: I aim to have unique entries for each date with all specials included. Desired Output

var totalUserJoined = [
    { 
        'Specials 5': 2192,
        'Specials 8': 767,
        'Specials 13': 2264 ,
        'dates': '2021-06-14'
    },
    { 
        'Specials 5': 2192,
        'Specials 8': 767,
        'Specials 13': 2264 ,
        'dates': '2021-06-16'
    },
    { 
       'Specials 5': 2192,
       'Specials 8': 767,
       'Specials 13': 2264 ,
       'dates': '2021-06-18'
    }
];

Answer №1

To extract all keys except for dates into their own object, you can utilize the combination of .map() and destructuring assignment. This will allow you to create an array of objects containing only the promotion keys excluding dates. Afterwards, you can merge these objects together using Object.assign() to form a single object. Subsequently, applying .map() on the original data array will enable you to combine the promotions object with each date:

const data = [ { 'Promotions 1': 2192, 'dates': '2021-02-04' }, { 'Promotions 23': 767, 'dates': '2021-02-06' }, { 'Promotions 12': 2264,'dates': '2021-02-08' }, ];

const promotions = Object.assign(...data.map(({dates, ...rest}) => rest));
const res = data.map(({dates}) => ({...promotions, dates}));

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

Encountering a problem with the state error while trying to modify an input field during onChange event

Whenever I pass state down from a parent component, the setState function keeps triggering an error that says setEmail is not a valid function on change event. Is there a simple solution to fix this issue? Every time I type a string into the email input f ...

The functionality of CSSTransition seems to be malfunctioning. Perhaps switching to V2 of react

Can anyone help me troubleshoot my code snippet where I'm attempting to incorporate an animation for Tooltip Nodes? Despite my efforts, the animation does not show up on the screen after mounting. Additionally, the console.log is not triggered on the ...

The function Server.listeners is not recognized by the system

Currently, I am following a tutorial on websockets to understand how to incorporate Socket.IO into my Angular project. Despite meticulously adhering to the instructions provided, I encountered an error when attempting to run my websockets server project: ...

Invalidating the memory in iOS 7.1.1 when using canvas drawImage

When I use the following code on an animation frame, I notice a significant memory leak that eventually causes IOS Safari or Chrome to crash: ctx.drawImage(anotherCanvas, clipX, clipY, clipW, clipH, x, y, w, h); Interestingly, if I don't apply a ...

Is there a way to customize the appearance of a MUI5 Tooltip using emotion?

I went through the information in this Stack Overflow post and experimented with the styled method. The code snippet I used is as follows: import * as React from 'react'; import { styled } from '@mui/material/styles'; import Tooltip, { ...

Refreshing Javascript with AngularJS

I'm encountering an issue while starting my angular js application. On a html page, I have divs with icons and I want the background color to change on mouse over. This is working using jquery's $(document).ready(function(){ approach. The conten ...

displaying a grey box on Google Maps using an *ngIf directive

I am working on a component that displays a map. @Component({ selector: "panda-map", template: ` <div class="map" [style.height.px]="height"> </div> ` }) export class PandaMapComponent implements OnInit { @Input ...

What is the best method to eliminate a "0" entry from a javascript event array?

Hello, I've got an array structured as follows: let test = ["testOne:,O,U,0","testTwo:R,C,0","testTree:1.334","testFour:r,z"]; I'm looking to iterate through the array and remove any occurrences of the cha ...

Specify that a pydantic object field should not be serialized if it is null

I need assistance with a pydantic object definition that includes an optional field. My goal is to configure the field in a way that it will only be serialized if it has a value other than None. class MyObject(BaseModel): id: str msg: Optional[str] = ...

Exploring the process of utilizing the graph API in order to post comments

Suppose I have 10 friends, how can I retrieve the most recent status updates for each of them and then leave a comment on all of these statuses? What is the method to obtain the post ID? $facebook->api('/'.$POST_ID.'/comments',& ...

Ways to collect email address or name from an email message

Suppose I send an email to someone with a link at the bottom. The text of the link might be something like click me. When the user clicks on this link, they will be directed to a webpage. On this webpage, a message saying "Thank you" will be displayed a ...

What are some ways to optimize my REST API requests on Android?

I am currently developing an Android app and I want to optimize the speed of my REST API calls. When I receive results in XML format, it takes a surprisingly long 5 seconds to retrieve even a simple XML structure like this: <souvenirs> <s ...

Utilizing PHP and JQuery Variables within the CodeIgniter Framework

Can someone please provide guidance on the best approach to handle this particular situation? I currently have a sidebar populated with Li Elements using a foreach loop, which is working perfectly. Each element contains a link that, when clicked, trigger ...

Steps to ensure a variable remains constant across a controller, JS file, and a rendered partial

I am working on rendering a partial viewer using ajax and jQuery. My goal is to assign a variable within the partial based on the link that is clicked. Here is the ERB code: <%=link_to "link1", viewer_path(id: Painting.first), remote: true%> <%= ...

Tips on how to choose all elements that do not include a specific value or group of values in cypress

Here's my situation: selector: ".list > div" const velue = [6, 9, 21] Each div contains a different value. I want to remove elements that contain a specific value, then select the first remaining element and click on it. It should look ...

Implementing styling upon app mounting in Vue.js - a step-by-step guide

Hey, I'm working with a template code that looks like this: Here's my script code: data () { return { loadPage: true } }, mounted () { this.loadPage = true }, And here is my styling code: #app{ width: 100%; opacit ...

What could be causing the issues with my Java parses and for loops not functioning properly?

I am currently developing a program to showcase arrays in a graphical user interface (GUI), but I have encountered some issues. The problem lies in parsing and the i variable within two for loops. As I am uncertain about the exact location of the issue, I ...

Change the data-theme using jQuery Mobile once the page has finished loading

I am facing an issue with my buttons in a control group that represent on/off functionality. Every time I click on one of the buttons to switch their themes, the theme reverts back once I move the mouse away from the button. How can I make sure that the ...

What is the best way to share models across different node.js projects?

In my setup, I have two node.js projects - project A and project B. Project A serves as the main project, while project B is more of an "ad-hoc" project with a specific purpose. The challenge lies in the fact that project B requires access to project A&apo ...

A method for utilizing wildcards in conjunction with the .load function

Currently, I am utilizing jquery, jquery mobile, js, html, and css within Phonegap to develop my android/ios application. However, my understanding of js & jquery is somewhat limited. In my index.html file, I use .load to load other html files into s ...