Divide and conquer Excel data with JavaScript based on a specific key

My current task involves extracting data from Excel using JavaScript. The extracted output is structured as follows:

{
    
    "Responsible": "Grey",
    "Goal": 0.823,
    "Jan": 0.8621397507374327,
    "Feb": 0.8605700219733681,
    "Mrz": 0.8870898346258058,
    "Apr": 0.8529801908413164,
    "Mai": 0.8507431241640211
  }

To enhance the presentation of this data, I aim to split it into smaller subsets like below:

         {   
            "Responsible": "Grey",
            "Goal": 0.823,
            "Month": 0.8621397507374327,  
          }


         {  
           "Responsible": "Grey",
            "Goal": 0.823,
            "Month": 0.8605700219733681,
         }
         
         {   
            "Responsible": "Grey",
            "Goal": 0.823,
            "Month": 0.8870898346258058, 
          }


         {  
           "Responsible": "Grey",
            "Goal": 0.823,
            "Month": 0.8529801908413164,
         }

         {  
           "Responsible": "Grey",
            "Goal": 0.823,
             "Month": 0.8507431241640211
         }
            

Below is a snippet of my code implementation for achieving this:

Ausgabe = [];     

_.forEach(data, element => {  
  obj = {
     
    Responsible: element.Responsible,
    Goal: element["Goal \r\n2022"]  ,
    Jan : element["Jan-22"],
    Feb: element["Feb-22"],
    Mrz : element["Mar-22"],
    Apr : element["Apr-22"],
    Mai : element["May-22"],
    
  }
  Ausgabe.push(obj)
    

})

The 'data' field holds the necessary information and can be manipulated if needed. It is expected to return the processed data upon completion.

Answer №1

Utilize destructuring for extracting Responsible, Goal, and the remaining properties of the object. Make use of Object.entries() to obtain an array of [key, value] pairs from rest, then iterate through it using Array.map() to form new objects:

const fn = ({ Responsible, Goal, ...rest }) =>
  Object.entries(rest)
    .map(([k, v]) => ({
      Responsible,
      Goal,
      [k]: v
    }))

const obj = { "Responsible": "Grey", "Goal": 0.823,  "Jan": 0.8621397507374327, "Feb": 0.8605700219733681, "Mrz": 0.8870898346258058, "Apr": 0.8529801908413164, "Mai": 0.8507431241640211 }

const result = fn(obj)

console.log(result)

If you are interested only in the month's values, opt for using Object.values() instead of entries:

const fn = ({ Responsible, Goal, ...rest }) =>
  Object.values(rest)
    .map(Month => ({
      Responsible,
      Goal,
      Month
    }))

const obj = { "Responsible": "Grey", "Goal": 0.823,  "Jan": 0.8621397507374327, "Feb": 0.8605700219733681, "Mrz": 0.8870898346258058, "Apr": 0.8529801908413164, "Mai": 0.8507431241640211 }

const result = fn(obj)

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

Creating custom markers with an API in Vue-2-Leaflet

I'm having trouble displaying markers using an API. I can retrieve the data from the API and store it in a variable, but for some reason the markers aren't showing up when I try to display them using a v-for loop. Any assistance would be greatly ...

Learn how to eliminate all text characters from a string using jQuery, leaving only the numerical values behind

My webpage features a variety of different products, each with their own values. When trying to calculate the total sum of these products and display it in the shopping cart, I encountered an error displaying NaN. How can I remove this NaN from the strin ...

Enhancing a three.js project with point-and-click functionality while utilizing OrbitControls

I am currently experimenting with a point-and-click navigation feature in three.js for a project. The goal is to move the camera to the location where the user clicks on the screen using a raycaster along with a simple mechanic implemented with gsap: ...

Issues with texturing in ThreeJS

I am currently working on a project in threejs that involves loading a .stl file. However, I have run into an issue where the loaded object automatically changes color from its original one. I would like to keep the original color of the object. What steps ...

Storing a Nested JSON Object in an ArrayList for MultiLevel ExpandableListView on Android

I am currently working with nested JSON data and a multi-level expandable list view. The structure of the JSON data is as follows: [ { "DivisionID": "2c0e9dc1-a6a7", "DivisionName": "Tyuio", "SubDivision": [ { ...

I'm currently experiencing an issue where I am not receiving the Validation Flash Message in the UI. Instead, I am seeing a flash error displaying as [object Object],[object

I am currently developing a Blog application using NodeJs, where I have integrated express Validator to validate data. I am facing an issue with displaying flash messages in the UI after submitting forms. The error message that I receive is [object Object] ...

Assigning a variable within a parent function from a child function in JavaScript

Struggling to assign the value of "result" in the inner function. Any suggestions on how to do this? I am able to console log the result variable inside the function, but a friend recommended using promises. However, I have no clue how to implement that ...

React Redux component fails to reflect changes in props after store update

Something odd is happening with my React Native component's props. They used to update after the redux store updated, but suddenly one day they stopped working. The dispatch is functioning correctly, and the store is updating state without any mutati ...

Using jQuery to show text upon hover using a `for` loop

I'm currently working on a webpage and incorporating a feature where hovering over specific div elements triggers the display of certain text in another div. There are 5 elements that I want to make hoverable and show text upon interaction. After imp ...

Retrieving event.data from the input handler on a textarea element

I've spent the past few days researching this issue. Here's some context to help explain the goal: I have a textarea and I want it to detect special characters (like @) that I define as part of a dictionary. It should then display an autocomple ...

What is the best way to extract data from a JSON-encoded string in PHP?

I came across a json structure which looks like this { "data": [ { "description": "~~ humorous ~~ for additional images, please visit www.1mpics.com Para mais imagens , visite www.1mpics.com", "media": { "image": { " ...

Header reference differs from the document referrer

this is the request header: GET / HTTP/1.1 Host: localhost:3002 Connection: keep-alive sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96" sec-ch-ua-mobile: ?0 User-Agent: ...

What is preventing the expected outcome of setting this as a background image in NextJS 13 with Tailwind CSS?

Can someone help me figure out how to achieve the desired effect for this component? import React from "react"; import Image from "next/image"; import heroBackground from "../public/hero-background.png"; const Hero = () => ...

Using Vue to append elements that are not part of the loop

While looping over a table row, I realized that each of the table rows should be accompanied by another table row containing additional data from the loop. Unfortunately, I am struggling to insert <tr class="data-spacer"></tr> <tr& ...

Is it possible to combine ng-switch and ng-if directives in Angular?

I attempted to combine the angular ng-switch and ng-if directives, but it doesn't seem to be functioning properly. Here is what I tried: <div data-ng-if = "x === 'someValue'" data-ng-switch on = "booleanValue"> <div data-ng-swit ...

Is it possible for the versions in the package.json file to be altered

I've made updates to my package.json file - all packages are listed as follows: "dependencies": { "@apollo/client": "3.6.4", "bootstrap": "4.6.2", "graphql": "16.5.0" } ...

Exploration into the Working Environments of JavaScript and Python

After executing JavaScript code (shown in the top-half) on link1 and python code (shown in the bottom-half) on link2, the diagram below was generated. My inquiry: I noticed that names foo & bar are already present in the global frame (highlighted in ...

The NodeJS module 'request' is producing symbols instead of expected HTML content

Currently, I am delving into the world of Nodejs and experimenting with web scraping using node.js. My tools of choice are the node modules request and cheerio. However, when I attempt to request a URL, instead of receiving the HTML body, I get strange s ...

Tips for ensuring that the dropdown is always visible in v-autocomplete/v-select

I'm currently working on a project utilizing the v-autocomplete component, which is actually an extension of the v-select element. In my scenario, I need to place this select element inside a v-menu container. When attempting to wrap a v-select with ...

Parsing string to JSON object and extracting specific information

In my code, I have a variable named "response" that contains the following string: {"test": { "id": 179512, "name": "Test", "IconId": 606, "revisionDate": 139844341200, "Level": 20 }} My goal is to extract the value of the id key and store ...