8
var enums = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
  };

I want to be able to convert that into an array that looks like this,

[
  {
    number:'1',
    word:'HELLO'
  },
  {
    number:'2',
    word:'BYE'
  },
  {
    number:'3',
    word:'TATA'
  }
]

all of the solutions I see form an array of either the keys or the values.

3
  • 4
    What is {'1', 'HELLO'}? It is neither valid JavaScript nor valid TypeScript. Commented Mar 5, 2019 at 17:27
  • Why do you expect {number:'2', word: 'TATA'}? Commented Mar 5, 2019 at 17:36
  • @jcalz obviously a typo Commented Mar 5, 2019 at 17:36

6 Answers 6

13

You could map the entries with short hand properties.

var enums = { 1: 'HELLO', 2: 'BYE', 3: 'TATA' },
    objects = Object.entries(enums).map(([number, word]) => ({ number, word }));

console.log(objects);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

7

You can use Object.entries and map it to desired format

var enums = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
  };
  
let op = Object.entries(enums).map(([key, value]) => ({ number:key, word:value }))

console.log(op)

6 Comments

how do you know this the expected output?
@brk this is specified in question mate. I want to be able to convert that into an array that looks like this,
Thanks for the answer. This gives me the output [ { "1": "HELLO" }, { "2": "BYE" }, { "3": "TATA" } ] I need it to be [ { "1","HELLO" }, { "2","BYE" }, { "3","TATA" } ] , instead of the :
@user7434041 can you describe the datatype you expect? { "1","HELLO" } is not valid javascript or typescript. it almost looks like a python tuple
@user7434041 in object you need key/value pair, in this format --> key:value.
|
3

Another alternative is to use a for ... in loop to iterate over the enums keys and construct your desired array of objects.

var enums = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
};

let res = [];

for (key in enums)
{
    res.push({number: key, word: enums[key]});    
}

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Comments

1

You can use Object.keys and map

var obj = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
};


const result = Object.keys(obj).map(el => {
  return {
    number: el,
    word: obj[el]
  }
})

console.log(result)

Comments

0

Need to create an object of Map type then get the value using get method like outData.get("1")

var obj = {
    '1': 'HELLO',
    '2': 'BYE',
    '3': 'TATA'
};
var outData = new Map();
Object.keys(obj).forEach(function (e) {
    outData.set(e, obj[e])
});

To get data use outData.get("key")

Now the out put data will like-

Map(3) {"1" => "HELLO", "2" => "BYE", "3" => "TATA"}

Comments

0

you can use Object.entries() with foreach and push it to an array like this

var enums = {
    '1': 'HELLO',
    '2' : 'BYE',
    '3' : 'TATA'
    };

var enumArray = []
Object.entries(enums).forEach(([key, value]) => enumArray.push({number : key, word : value}));

console.log(enumArray);

1 Comment

this will give you all key and value combinations resulting in wrong number of array objects E.G typescriptlang.org/play?#code/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.