A method for replacing keys within an object with those from another object

I am looking for a way to remove keys from objects within another object using TypeScript.

let before = {
  '0': {
    'title':'title 1',
    'time':'12.30pm',
  },

  '1': {
    'title':'title 2',
    'time':'12.30pm',
  },

  '2': {
    'title':'title 3',
    'time':'12.30pm',
  },
}

The desired outcome is,

let after = [
  { 
    'title':'title 1',
    'time':'12.30pm',
  },

  {
    'title':'title 2',
    'time':'12.30pm',
  },

  {
    'title':'title 3',
    'time':'12.30pm',
  }
]

Answer №1

Simply utilize Object.values(before)

let before={0:{title:"title 1",time:"12.30pm"},1:{title:"title 2",time:"12.30pm"},2:{title:"title 3",time:"12.30pm"}};


console.log(Object.values(before));

Answer №2

To achieve the desired outcome, consider utilizing the following method involving Object.entries and map

  1. Object entries will provide an array of key-value pairs from the object
  2. Using map to return the values

let sampleObj = {
  '0': {
    'title':'title 1',
    'time':'12.30pm',
  },

  '1': {
    'title':'title 2',
    'time':'12.30pm',
  },

  '2': {
    'title':'title 3',
    'time':'12.30pm',
  },
}

console.log(Object.entries(sampleObj).map(val => val[1]))

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

Having trouble loading script files with JSDOM in NodeJS?

I'm currently experimenting with loading an HTML page in jsdom to generate graphs, but I'm facing challenges in getting the JavaScript to execute. Here's the HTML page I'm trying to load, which simply renders a basic graph: <html&g ...

How can we define a function using a generic type in this scenario using Typescript?

Here's a challenge that I'm facing. I have this specific type definition: type FuncType<T> = (value: T) => T I want to create a function using this type that follows this structure: const myFunc: FuncType<T> = (value) => valu ...

For each individual category in the mongoose database, conduct a search

Seeking assistance with mongoose scope issue User.findById(req.user.id, (err, result) => { var cartProduct = []; result.cart.map(async (item) => { //item represents the following object: productId: "product-id", quan ...

Automatically change and submit an <input> value using jQuery

I have been working on a prototype to enable users to access all the data associated with a visualization. The idea is that they would click on a specific point, triggering a modal to appear, and within that modal, a table would dynamically filter based on ...

What is the best way to define the active <li> tab using JQuery?

Initially, my HTML code includes a set of <li> elements that I want to dynamically add to the existing <ul>. <ul class="tabs" id="modal_addquestion_ul"> <li class="tab-link current" data-tab="multipleChoice">Multiple Choice< ...

Is it possible to trigger the setState() function of a parent component when a child component is clicked?

Hey there, I'm a new developer diving into the world of Reactjs. I've been working on setting up a Todo app but struggling to configure it just right. My main challenge is getting a button to add items to the list when submitted. I think I'm ...

Modify a field within MongoDB and seamlessly update the user interface without requiring a page refresh

I am currently working on updating a single property. I have various properties such as product name, price, quantity, supplier, and description. When sending the updated quantities along with all properties to MongoDb, I am able to update both the databas ...

Is it possible to save a dynamic web page to a file?

I am a beginner C++ programmer diving into the world of web development for the first time. My current challenge involves finding a way to continuously capture and save the HTML content of a constantly updating third-party website onto a static HTML file ...

What is the best way to transition this endpoint from JavaScript to TypeScript?

I'm in the process of building a chat application with the t3 stack. I've successfully created a socket endpoint using JavaScript, but now I'm facing some challenges as I try to convert it to TypeScript. import { Server } from "Socket.I ...

Tips for transferring an object as a prop in React with the help of typescript

Learning TypeScript has been a challenge for me, as I struggle to grasp the concepts. Despite the countless times this question has been asked before, I find it difficult to apply the solutions I come across to my own code. To make matters worse, the synta ...

When incorporating a Textarea element within styled components in ReactJS, it causes the text to be inputted backwards due to the cursor

Currently, I am utilizing the Textarea component from react-textarea-autosize along with using styled from styled components. In a class, I have created a function called renderForm which is responsible for rendering a form containing a StyledTextarea. How ...

Tips on changing a div HTML tag through a button's onclick event with javascript

<nav class="navbar navbar-default" style="..."> <div class="container" id="main_container" style="margin-top: -80px"> <div> <div class="row"> <div class="col-xs-0 col-md-2" style="backgroun ...

When attempting to use Arabic characters in my PDF file with LIP, I encounter an error: "TypeError: font must be of type PDFFont or n, but was actually of type NaN"

all I am facing an issue with Arabic characters when trying to use different fonts in my project. I have attempted to encode the Arabic letters using various npm packages, but I keep receiving a TypeError: font must be of type PDFFont or n but was actuall ...

Exploring ways to implement identical 'if' conditions across various controllers in AngularJS

Is there a way to avoid repeating the if condition in multiple controllers? How can I simplify this process? MainController.js : $scope.responseData.forEach(function(card) { switch (card.Category) { case 'Apple': conso ...

The program performs flawlessly on two occasions, but then it starts to malfunction

Even after inputting the correct answer multiple times, the output still displays "try again." What could be causing this unusual behavior? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit ...

ADVENTURE BLOCKED - Intercept error: net::ERR_BLOCKED_BY_CLIENT

I've encountered an issue where my initialize function doesn't run properly when a user has an ad blocker enabled. It seems that the ads-script.js file is being blocked by the client with a net::ERR_BLOCKED_BY_CLIENT error, leading to my .done ca ...

Using JavaScript to populate an input field with a value

I am having an issue with a Javascript code that is supposed to update a form input value, but for some reason it is not working as expected. Below is the code in question: <script> function up2(mul) { var passcode = parseInt(document.getElementById ...

Exploring directory organization in GraphQL Queries using GatsbyJS

In my portfolio, I have organized my work into categories, pieces, and pictures in a cascading order similar to a child-parent relationship. The folder structure reflects this hierarchy, with the main problem being explained in more detail below. Folder s ...

Update the PHP code every second

I have a log-in screen visible in an iframe. When someone logs-in on the iframe, a menu-button should pop up on the main page. Here is my current code snippet: <?php if (isset($player)){ echo' <ul class="n ...

Encountering the error message "import statement cannot be used outside a module" when using an NPM package

I recently tried to incorporate the Typed.js library into my project by installing it through NPM. After setting up a simple example in my "main.js" file and linking it with <script src="main.js"></script>, I encountered the error message Error ...