Using Lodash to create a fresh array by utilizing the values that already exist in another array

Here is an example of an array:

[{
        "first_name": "Anna",
        "last_name": "William",
        "class": "math",
        "year": "1990"
    },
    {
        "first_name": "Tom",
        "last_name": "Cruise",
        "class": "biology",
        "year": "1991"
    }
]

I want to convert it into a new array as shown below:

[{
        "name": "Anna William",
        "class": "math"
    },
    {
        "name": "Tom Cruise",
        "class": "biology"
    }
]

I could use a loop to achieve this, but it would be lengthy and not very elegant. I am looking for suggestions on how to transform the array using Lodash functions for shorter and more readable code. Any advice would be appreciated!

Answer №1

To transform an array and extract specific values, you can utilize the .map method

let data = [{"first_name": "Anna","last_name": "William","class": "math","year": "1990"},{"first_name": "Tom","last_name": "Cruise","class": "biology","year": "1991"}]

let result = data.map(item => ({name: `${item.first_name} ${ item.last_name}`, subject:item.class }))

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

Angular.js repeatedly requesting data without ever successfully loading it

I'm currently learning Angular.js and have set up a Node app to serve a basic Angular web page with log data being written to stdout. Everything was running smoothly until recently when, for unknown reasons, the page started crashing every time I trie ...

Encountering difficulties in constructing next.js version 14.1.0

When attempting to build my next.js application, I use the command npm run build Upon running this command, I encountered several errorshttps://i.sstatic.net/5jezCKHO.png Do I need to address each warning individually or is there a way to bypass them? B ...

Navigate the JSON object at predetermined intervals, such as in the case of movie subtitles

Apologies if the title is not specific enough, open to any suggestions for improvement. Here's my issue: I have a JSON file (presented here as a JavaScript object) that contains subtitles for a movie. My goal is to display the text exactly as it appea ...

Menu secured in place within the wrapper

My website is contained in a wrapper with a max width, and I have a fixed side menu that can be toggled with a button. The problem I am facing is keeping the fixed side menu within the page wrapper. Fixed elements are typically positioned relative to the ...

Creating a customized tooltip without the need for calling $(document).foundation() and using modernizr

I'm facing an issue with initializing the foundation tool-tip without having to initialize everything (such as $(document).foundation()). It seems like a simple task, but I am struggling. I have two questions in mind: (1) How can I utilize new Founda ...

An error in the floating point has occurred while attempting to generate prime numbers

My program is capable of generating prime numbers. The code functions as expected when I request the first 100 or 200 primes, but encounters a Floating Point Exception error when I attempt to generate prime numbers beyond 300. It appears that the issue l ...

Oops! We couldn't locate or access the file you're trying to import: ~bootstrap/scss/bootstrap

Following the steps outlined on getbootstrap.com, I assumed that everything would function smoothly. Unfortunately, that hasn't been the case so far. All seems well until I attempt to load the page, at which point my Express.js app throws a: [[ ...

What methods can I employ JSON to create a dynamic Sidebar within Nextjs?

[ { "type": "root", "children": [ { "type": "file", "route": "Open-EdTech/AWS-Associate-Notes/EC2.md", "title": "EC2", ...

Files with extensions containing wildcards will trigger a 404 error when accessed from the public folder in NextJS

I have successfully set up my public folder to serve static files, however I am encountering an issue with files that have a leading dot in their filename (.**). Specifically, I need to host the "well-known" text file for apple-pay domain verification, wh ...

Unable to retrieve a value after deactivating a Div element

I have created a web page using MVC Razor. On this page, I included a div element with the following structure: <div class="row" id="DivDisableForView"> <div class="col-md-4"> <label for="" class="control-label" ...

Can you create reusable components in Wordpress that are encapsulated?

In my quest to explore innovative approaches to Wordpress theme development, I have stumbled upon a variety of options such as the "Roots Sage" starter theme, the "Themosis Framework," and "Flynt." While these solutions address intriguing problems, they do ...

Progress Bar Has Wandered Off Track

Having trouble with two queries. Take a look at the live view here First Query: Struggling to position the progress bar below my image using CSS. Second Query: Want to make the images slide left instead of fading with the progress bar in my Javascript. ...

Display a loader while waiting for an API call to complete within 5 seconds using Angular and RxJS operators. If the API call takes longer

We are actively working to prevent user blockage during file uploads by implementing a method in Angular using RxJS. How can I display a toastr message and hide the loader if the API does not complete within 5 seconds? uploadFile() { this.service.uploa ...

Streamlining Complex Javascript IF Statements

I came across a suggestion online for making an if statement more concise: if([1,5,7,22].indexOf(myvar)!=-1) alert('yeah') Instead of the longer version like this: if( myvar==1 || myvar==5 || myvar==7 || myvar==22 ) alert('yeah') Is ...

What is the best way to modify a newly added element using jQuery?

When a user clicks a button on my screen, a new template is loaded dynamically using jQuery's .load() method. This transition adds new HTML to the page that was not present when the script initially loaded: (function($) { $('.go').on("c ...

What might be causing my AJAX success callback function to not function as anticipated?

I've written this code for an ajax request, but I'm struggling to figure out why the code inside the success method isn't functioning properly. Even though the network tab in my Chrome browser shows a state of 200 OK. Here is the ajax code ...

Pinia has not been instantiated yet due to an incorrect sequence of JavaScript file execution within Vue.js

I'm currently developing a Vue.js application using Vite, and I have a Pinia store that I want to monitor. Below is the content of my store.js file: import { defineStore } from 'pinia'; const useStore = defineStore('store', { st ...

Is there a vulnerability in .NET when converting strings to byte arrays?

Encountered an issue while retrieving encrypted data from an NVARCHAR field in our SQL Server (2008R2) database. It seems that for certain records, the string value of the data in my C# .NET application differs from that in the database record. After some ...

Why is my output getting mixed up with the header on the navigation bar page?

output capture image https://i.sstatic.net/BYJnk.jpg here is the code snippet //ui.r library(shiny) navbarPage(theme = "style.css",img(src="logo.jpeg", width="300px"), tabPanel("Dashboard"), tabPanel("Products"), tags$head( t ...

A guide on arranging map entries based on their values

The map displayed below, as represented in the code section, needs to be sorted in ascending order based on its values. I would like to achieve an end result where the map is sorted as depicted in the last section. If you have any suggestions or methods o ...