0

Consider the following object

var obj1=[
    {
      'Charles Byers': 'Industrials',
      'David Smith': 'Industrials',
      'Joe Evans': 'Industrials',
      'Larry Young': 'Industrials',
      'Martin Hammond': 'Industrials',
      'William Green': 'Industrials'
    }
  ]

I need to remove the starting and ending square brackets [] without converting this into a string Like,

var obj1=
          {'Charles Byers': 'Industrials',
          'David Smith': 'Industrials',
          'Joe Evans': 'Industrials',
          'Larry Young': 'Industrials',
          'Martin Hammond': 'Industrials',
          'William Green': 'Industrials'}
       

How can I possibly achieve this?

NOTE: The size and contents of the object might change.

1 Answers1

1

You could get the entries and map new objects from a single key/value pair.

const
    object = { 'Charles Byers': 'Industrials', 'David Smith': 'Industrials', 'Joe Evans': 'Industrials', 'Larry Young': 'Industrials', 'Martin Hammond': 'Industrials', 'William Green': 'Industrials' },
    result = Object
        .entries(object)
        .map(keyValue => Object.fromEntries([keyValue]));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392