JavaScript function that takes an array of large objects and returns an array of smaller objects

Consider this scenario: I have a collection of large objects consisting of various attributes like ID, type, title, count, and more.

{
    "id": "0165a74c-2545-40f7-9145-b95f5e5cb77e",
    "type": 4,
    "title": "My Title",
    "delete": false,
    "delete_reason": null,
    "delete_time": "2019-12-05T16:17:15.313Z",
    "count": 37765,
    "sync": 1575672973,
    "observe": 1575672949,
    "updated": true,
    "option": null
}

By using obj.filter(), I can selectively filter the objects based on their type 'n'.

However, even after filtering, the resulting array still contains the entire large objects.

If I want to extract only the title and ID from these objects to create a new array, how can I achieve that?

[
    {
         "id": "0165a74c-2545-40f7-9145-b95f5e5cb77e",
         "title": "My Title"
    },
    ...
]

Is there a way to accomplish this using map reduce, or do I need to revert to using a for loop for this task?

Answer №1

To extract just the attributes, you can utilize destructuring and create new objects using map method.

output = items.map(({ name, price }) => ({ name, price }));

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

Steps for establishing a thread pool for workers

I have been exploring the experimental functionality of the worker threads module in Node.js. After reading through the official docs and various articles (although limited in number), I decided to create a simple example. This example involves spawning te ...

Tips for ensuring synchronous state changes in React

I am working on a currency calculator using react.js I am fetching the latest exchange rates and storing them in state using the getRates function: getRates(currencyShortcut){ fetch('https://api.fixer.io/latest?base='+currencyShortcut) ...

Competition among fetch requests

I am currently developing a tracker that is designed to gather data from our clients' websites and send it to our API using fetch requests when site users navigate away from the page. Initially, I planned to utilize the beforeunload event handler to ...

retrieve a reply from a PHP script and incorporate it into JavaScript following an AJAX request

I've been working with an ajax call and everything seems to be running smoothly thanks to this script: $(document).ready(function() { $('.sortable').sortable({ stop: function(event, ui) { $(ui.item).effect("highlight"); ...

How to eliminate certain elements from the DOM in Angular 5

I'm facing a minor issue in my Angular/Typescript project that I can't seem to resolve. I am still new to these technologies and struggling with removing certain DOM elements. The content is auto-generated with specific CSS classes, and unfortuna ...

What is the best method for incorporating a CSRF token into a JavaScript file?

My Node.js application has CSRF implemented, and it was working well when I had JavaScript inline in a JADE file. I just used #{token} to insert the token into the JavaScript code. But now that I have moved my JavaScript into external files, I am struggli ...

Guide to obtaining ngPrime autocomplete text when the button is clicked within Angular 6

I am currently utilizing the ngPrime autocomplete feature to retrieve data from a service. My goal is to capture the text entered in the autocomplete field whenever a button is clicked. I attempted to access the value using getElementById.value within th ...

ES6 syntax specification allows for the use of a fat arrow function for declaring React components

When learning React, I have noticed two different ways of declaring components. The first is using the classic fat arrow syntax with a return statement. const Component = () => { return ( <div>Hello</div> ) } Recently, I came ...

Exploring the possibilities of PHP, AJAX, and JSON

I am currently exploring the functionalities of ajax and json. Within my page named addAccount.php, I have a FORM containing the following INPUT elements: <input type="text" id="partnerCode" /> <input type="button" id="pCodeSearch" value="Sear ...

Deleting a hyperlink within a content-editable area

Presently, I am in the process of working on a straightforward project where users can format text using contenteditable. I have successfully implemented a feature that allows users to add hyperlinks to selected text by clicking on the "Create Link" button ...

Concealing and revealing the sidebar container

I created a website here and needed to implement a button in the header to hide and show the sidebar. Unfortunately, my current code is not working. Here is what I have attempted: <div id="B"> <button>toggle</button> </div> $ ...

the nextjs model is throwing a 400 bad request error when fetching data

I need some assistance with my web application. Whenever I try to create a record by sending data to the API endpoint, it always returns a 400 bad request error on the front end. Surprisingly, when I tested the same request in Insomnia, everything worked p ...

`Why my React function calls are not being tested properly with Jest and Sinon?`

I need help determining how many times my React class method is executed after an event. I've experimented with sinon.spy and jest.fn() but haven't had success. Attempt using sinon.spy: test('Example test', (done) => { const page ...

Simplified jQuery function for multiple div mouseover operations

Uncertain about the accuracy of my title. Due to certain reasons, I need to assign different IDs for the class, as it only detects ID and not class when hovered over. Therefore, I have created a CSS version where the opacity of a specific div will change t ...

Utilize array.prototype.reduce() with strings

I'm puzzled about how the reduce operation is carried out on a string. Initially, a new Str instance is created with the desired string as a parameter. Following that, the split method is used to divide the string into an array of strings. A method c ...

Ways to display a targeted div element in Ruby on Rails when a limit is imposed

I'm working on a Rails app and I have a specific goal in mind. I want to automatically display a static div element only when the conditions limit(4) are met, meaning 4 div elements are showing. Essentially, I want to show the "Apply for our Program" ...

Using THREE.js to cast rays from a secondary camera onto the scene

I've been attempting to raycast the mouse from my camera in order to trigger hover and click events on meshes within my scene. The issue I'm facing is that my camera is currently a child object of another mesh (to facilitate easier camera moveme ...

Unlimited digest loop in Angular.js caused by nested ng-repeat and filter

In my AngularJS project, I have developed a custom filter that categorizes elements by type and performs a search for multiple search terms across all attributes of devices. angular.module('abc').filter('searchFor', function(){ return ...

When trying to gather multiple parameters using @Param in a NestJS controller, the retrieved values turn out

Can someone help me understand why I am struggling to retrieve parameters using the @Param() decorators in my NestJS controller? These decorators are defined in both the @Controller() decorator argument and the @Get() argument. I am relatively new to Nest ...

Animation displayed on page load before running

Is there a way to ensure that my animation runs smoothly without any lag or jankiness, either while the page is loading or immediately after loading? I have attempted to preload the images using a preload tag in the header of my HTML, but unfortunately, t ...