1

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'.

6 Answers 6

1

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);

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

3 Comments

Would it be possible to change the order of the data by a given label order? For example if I want to get the 'out' value at first position and of course there are more data values...
@user3142695 yeah, it is possible
But not with the reduce function as it is used in your answer, isn't it?
1

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);

Comments

1

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)

Comments

0

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)

Comments

0
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
  ]
}

Comments

0
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: []
    })

Comments

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.