Arranging an array of objects in a structured format

I'm working with an array of objects that looks like this

const obj = [{ '1': 'a'},{ '2': 'b'}, {'3': 'c'}] 

and I need to extract the keys and values separately, like so: ['1','2','3'] and ['a','b','c']

Can anyone provide guidance on how to achieve this?

I've attempted it but haven't been able to get the desired outcome.

Answer №1

Here is how you can achieve this:

const arr = [{ '1': 'a' }, { '2': 'b' }, { '3': 'c' }]

console.log(arr.flatMap((obj) => Object.keys(obj)))
console.log(arr.flatMap((obj) => Object.values(obj)))

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

What is the best way to assign values to an entire row in a dynamic two-dimensional array?

My task involves carrying out 9 specific operations on a coordinate based on its position. I have a function that provides the coordinates of surrounding positions (down, up, left, right, or diagonals) relative to the given coordinate. These 9 different op ...

Tips for choosing elements from an array that contain a number within a specific range in MATLAB

I have the following array data: AB01 4 7 AB02 3 4 AB02 2 4 AB03 9 5 AB01 3 3 AB04 3 2 AB05 4 1 AB03 4 1 AB05 3 4 AB04 1 5 In my scenario, I am provided with two number inputs that determine a minimum and ...

Using angular.forEach and console.log to iterate over arrays in AngularJS can lead to unexpected results

This application is designed for guessing zip codes, using FireBase and Angular technologies. Whenever the user inputs a digit, a query is sent to a FireBase database which returns an array of zip codes containing that specific digit in the corresponding p ...

Using MySQL or PHP to organize and style groups with tags

Suppose we have an array setup like this pulled from a mysql function: function getGroups($limit = 10) { $data = $this->fetchAll ( 'SELECT gid, `group`, information, tag FROM groups GROUP BY tag LIMIT ' . $limit ); return $data; ...

How to achieve two-way binding using @Prop() in Vue Cli 3 with TypeScript?

Here is the source code snippet: This is the Child Component: <template> <v-snackbar v-model="showSnackbar" :bottom="y === 'bottom'" :left="x === 'left'" :multi-line="mode === 'multi-line'" :ri ...

The act of exporting an enum from a user-defined TypeScript path leads to the error message "Module not

I have set up a custom path as explained in this particular discussion. "baseUrl": ".", "paths": { "@library/*": [ "./src/myFolder/*" ], } Within this module, I am exporting an Enum. export enum EN ...

Is there a shorter method to confirm that all elements in an array satisfy a certain condition?

It is widely understood that exact equality should not be asserted for floating-point numbers. assertk offers a more convenient approach: assertThat(value).isCloseTo(3.0, 0.000001) I am interested in extending this functionality to arrays: assertThat(arr ...

Typescript is throwing a Mongoose error stating that the Schema has not been registered for the model

I've dedicated a lot of time to researching online, but I can't seem to figure out what's missing in this case. Any help would be greatly appreciated! Permission.ts (This is the Permission model file. It has references with the Module model ...

Unexplained Reference Error in Next.js Typescript: Variable Accessed before Initialization

I am currently working on an admin website and encountered the error Block-scoped variable used before its declaration.. I will provide details using images and code. This is my first time seeking help on StackOverflow. Error Message: Block-scoped variab ...

What steps should I take to allow my code in an AWS Fargate container to take on an IAM role while accessing S3 using Node.js?

I've been working on implementing IAM roles to manage S3 access in my application, but I seem to be missing a crucial step. While running my code in AWS, I encountered a "missing credentials" exception, indicating that something is not configured corr ...

Angular Service: Circular Dependency Problem Explained

Greetings, I am fairly new to the realm of Angular and have some background in AngualarJS (not very helpful here hahaha). Currently, I am referring to this resource to implement a Service/State for a specific Module. However, when attempting to use it wi ...

Variety of returns from shared functions

class Parent { protected info: any; getInfo(): dataTypeA | dataTypeB { return this.info; } } class A extends Parent { protected info: dataTypeA = getDataTypeA(); } class B extends Parent { protected info: dataTypeB = getDataTypeB ...

Create a PHP array from a MySQL database using the "id" column as the key value pairs

My MySQL database structure is as follows: ID TEXT PARENTID 20 Item1 null 23 Item2 20 27 Item3 20 80 Item4 27 I am aiming to retrieve this data in an array format like so: Array ( [2 ...

Exploring the Power of Node.JS in Asynchronous Communication

Hey there, I'm not here to talk about async/await or asynchronous programming - I've got that covered. What I really want to know is if it's possible to do something specific within a Node.js Express service. The Situation I've built ...

What is the best way to modify the KeyName in an object?

Having difficulty parsing an Object by changing keynames due to the error message "Element implicitly has an 'any' type because expression of type 'keyof SignInStore' can't be used to index type '{}'". interface SignInSto ...

Leverage the template pattern in React and react-hook-form to access a parent form property efficiently

In an effort to increase reusability, I developed a base generic form component that could be utilized in other child form components. The setup involves two main files: BaseForm.tsx import { useForm, FormProvider } from "react-hook-form" expor ...

Receive a pair of distinct arrays through a text field submission

I have a form on my website with a single textarea input. Visitors can enter text in the textarea in the following format: 5x Blue Flower 2 Red Flower 3* Yellow Flower Purple Flower I need to extract two arrays from this input - one for the quantity an ...

Guide to highlighting manually selected months in the monthpicker by utilizing the DoCheck function in Angular

I'm facing an issue and I could really use some assistance. The problem seems quite straightforward, but I've hit a roadblock. I have even created a stackblitz to showcase the problem, but let me explain it first. So, I've developed my own t ...

Tips for resetting an RXJS scan operator depending on a different Observable

I created a component that triggers an onScrollEnd event once the last item in a virtual list is displayed. This event initiates a new API request to fetch the next page and combine it with the previous results using the scan operator. In addition, this c ...

What is the best way to fetch only *.txt file names from a directory using the C programming language and store them in a char

In continuation of my previous inquiry: How can I retrieve only txt files from a directory in C?, I am now looking to store the filenames (the quantity of which is unknown) in a char ** array. While I initially came up with a solution (sort of), I realized ...