user3142695
user3142695

Reputation: 17332

Transform array into object with array values

I need to transform this array

[ { _id: 'indoor', count: 12 }, { _id: 'outdoor', count: 34 } ]

into this result:

{ label: ['in', 'out'], data: [12, 34]}

So I need to get the labels (_id) in an array and replace them with some individual text and I need to get the values as data array. The order of both should be the same of course.

For the second part I would do something like

array.map(c => c.count)

I can do the same thing with the label value, but how do I use individual text? E.g. 'indoor' should be replaced by 'car' and 'outdoor' should replaced by 'plane'.

Upvotes: 1

Views: 98

Answers (6)

StepUp
StepUp

Reputation: 38094

You can use reduce method:

let sourceData = [
    { _id: 'indoor', count: 12 },
    { _id: 'outdoor', count: 34 }
];

const maps = {indoor: 'in', outdoor: 'out'};    

const result = sourceData.reduce((a, c) => {
    a.label = a.label || [];
    a.data = a.data || [];

    a.label.push(maps[c._id]);
    a.data.push(c.count);

    return a;
}, {});

console.log(result);

You can sort your desired array in custom order:

let sourceData = [
    { _id: 'indoor', count: 12 },
    { _id: 'outdoor', count: 34 }
];

const maps = {indoor: 'in', outdoor: 'out'};    

const result = sourceData.reduce((a, c) => {
    a.label = a.label || [];
    a.data = a.data || [];

    a.label.push(maps[c._id]);
    a.data.push(c.count);

    return a;
}, {});

result.label.sort((a, b) => {
  const order = {'out': 1, 'in': 2, undefined: 3};
  return order[a] - order[b];
});

console.log(`custom order of labels: `, result);

Upvotes: 1

Federico
Federico

Reputation: 3920

const arr = [ { _id: 'indoor', count: 12 }, { _id: 'outdoor', count: 34 } ];   
arr.map(o => [o._id === 'indoor' ? 'in' : 'out', o.count])
    .reduce((p, c) => ({
        label: [...p.label, c[0]]),
        data: [...p.data, c[1]])
    }), {
        label: [],
        data: []
    })

Upvotes: 0

OlegI
OlegI

Reputation: 6010

const data = 
[ 
  { _id: 'indoor', count: 12 }, 
  { _id: 'outdoor', count: 34 } 
]
const result = {
  label: data.map(a => a._id.replace('door','')),
  data: data.map(e => e.count)
}

console.log(result)

Upvotes: 1

Nenad Borovčanin
Nenad Borovčanin

Reputation: 9

const data = [ { _id: 'indoor', count: 12 }, { _id: 'outdoor', count: 34 } ];

const indoor = data.filter((item) => item._id === 'indoor').map((i) => i.count);
const outdoor = data.filter((item) => item._id === 'outdoor').map((i) => i.count);

const res = { label: ['in', 'out'], data: [...indoor, ...outdoor]};

console.log(res);

Will give you next:

{
  label: [
    "in",
    "out"
  ],
  data: [
    12,
    34
  ]
}

Upvotes: 0

brk
brk

Reputation: 50291

In the map call back you can check the text and return the required string

let data = [{
  _id: 'indoor',
  count: 12
}, {
  _id: 'outdoor',
  count: 34
}]

let newData = {
  label: data.map((item) => {
    if (item._id === 'indoor') {
      return 'in';
    } else {
      return 'out';
    }

  }),
  data: data.map(item => item.count)

};
console.log(newData)

Upvotes: 0

junvar
junvar

Reputation: 11574

let labels = {
  indoor: 'car',
  outdoor: 'plaine'
};

let input = [{
  _id: 'indoor',
  count: 12
}, {
  _id: 'outdoor',
  count: 34
}];
let output = input.reduce((output, inObj) => {
  output.label.push(labels[inObj._id]);
  output.data.push(inObj.count);
  return output;
}, {
  label: [],
  data: []
});

console.log(output);

Upvotes: 1

Related Questions