Tutor ClassD
Tutor ClassD

Reputation: 21

How to convert an array of multiple entries into an object?

const array = [['Team A', 'City A', '100'],['Team B', 'City A', '80'],['Team C', 'City A', '90']];
const map = array.map(x => {
  let output = {};
  output[x[0]] = {team: x[0], city: x[1], points: x[2]};
  return output
});
console.log(JSON.stringify(map));

And I'm getting output result as below

[{"Team A":{"team":"Team A","city":"City A","points":"100"}},
 {"Team B":{"team":"Team B","city":"City A","points":"80"}},
 {"Team C":{"team":"Team C","city":"City A","points":"90"}}]

And required out is

{"Team A": {"team": "Team A","city": "City A","points": "100"},
 "Team B": {"team": "Team B","city": "City A","points": "80"},
 "Team C": {"team": "Team C","city": "City A","points": "90"}}

Upvotes: 2

Views: 116

Answers (5)

3limin4t0r
3limin4t0r

Reputation: 21110

I'm surprised a simple for...of loop isn't mentioned yet.

const array = [['Team A', 'City A', '100'],['Team B', 'City A', '80'],['Team C', 'City A', '90']];

const output = {};
for (const [team, city, points] of array) {
  output[team] = {team, city, points};
}

console.log(output);

The reason why your current code doesn't work is because you call array.map, which by nature returns a new array, not an object. Furthermore you create a new output object in each array.map iteration, whereas there should only be one output object, holding the different teams.

Note that this answer makes use of destructing assignment and the ES2015 object literal notation.

Here is a version that is more similar to what you currently have, but uses forEach instead of map. This assigns output before iteration, updating output each iteration.

const array = [['Team A', 'City A', '100'],['Team B', 'City A', '80'],['Team C', 'City A', '90']];

const output = {};
array.forEach(x => {
  output[x[0]] = {team: x[0], city: x[1], points: x[2]};
});

console.log(output);

Upvotes: 1

Scott Sauyet
Scott Sauyet

Reputation: 50787

This is similar to several other answers, but without mutating data structures along the way:

const restructure = (teams) =>
  teams .reduce ((a, [team, city, points]) => ({...a, [team]: {team, city, points}}), {})

const array = [['Team A', 'City A', '100'],['Team B', 'City A', '80'],['Team C', 'City A', '90']];

console .log (restructure (array))

This is far from the most performant code. An excellent article from Rich Snapp explains why and how to fix it. But I personally would stick with this unless this becomes a bottleneck in my application.

Upvotes: 4

Orelsanpls
Orelsanpls

Reputation: 23515

Array.reduce is going to loop through the original array and build a new object. We destructure the argument in order to quickly create our new key using the position of the data inside of the original array. Then we use the syntax , to say "assign the key, then return tmp variable"

const format = [
  [
    'Team A',
    'City A',
    '100',
  ],
  [
    'Team B',
    'City A',
    '80',
  ],
  [
    'Team C',
    'City A',
    '90',
  ],
].reduce((tmp, [
  team,
  city,
  points,
]) => (tmp[team] = {
  team,
  city,
  points,
}, tmp), {});

console.log(format);

Upvotes: 3

Nikhil Goyal
Nikhil Goyal

Reputation: 1973

Please use the below snippet

function mapArray(arr) {
   return arr.reduce((acc, curr) => {
    const [ team, city, points ] = curr;
    acc[team] = {
      team, city, points
    };
    return acc;
   }, {});
}

const array = [['Team A', 'City A', '100'],['Team B', 'City A', '80'],['Team C', 'City A', '90']];

console.log(mapArray(array));

Upvotes: 1

ggorlen
ggorlen

Reputation: 56875

You can use reduce to make an object like this (map gives you an array):

const array = [['Team A', 'City A', '100'],
               ['Team B', 'City A', '80'],
               ['Team C', 'City A', '90']];
const result = array.reduce((a, [team, city, points]) => {
  a[team] = {team, city, points};
  return a;
}, {});
console.log(JSON.stringify(result, null, 2));

Upvotes: 3

Related Questions