0

I have the following expression:

export const alpha: [
    { 'A': ['11','12','13'] },
    { 'B': ['21','22','23'] },
    { 'C': ['31','32','33'] }
];

I need to create a new array that has the leading keys: 'A', 'B' and 'C'

I'm trying to do this, but there is no key property:

const arr = [];
alpha.forEach(function(item) {
  arr.push(item.key);
});

How to do it?

0

2 Answers 2

2

One approach would be to use Object.keys():

const alpha = [
    { 'A': ['11','12','13'] },
    { 'B': ['21','22','23'] },
    { 'C': ['31','32','33'] }
];

const arr = [];

alpha.forEach(function(item) {
  arr.push(Object.keys(item)[0]);
});

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

0

I would build it up deconstructing the array like so:

let newArray6 = {'A':[ ...alpha[0].A],'B':[ ...alpha[1].B],'C':[ ...alpha[2].C]}

This could then be easily turned into a programmatic loop :)

The output would be as follows:

A: (3) ["11", "12", "13"]
B: (3) ["21", "22", "23"]
C: (3) ["31", "32", "33"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.