4

I have a object like this

{
"User 1":[
{"count":"1","stage":"A","jCount":"10","name":"User 1","stageId":"A1"},
{"count":"8","stage":"B","jCount":"10","name":"User 1","stageId":"B1"},
],

"User 2":[
{"count":"7","stage":"C","jCount":"8","name":"User 2","stageId":"C1"},
{"count":"8","stage":"B","jCount":"8","name":"User 2","stageId":"B1"},
{"count":"9","stage":"A","jCount":"8","name":"User 2","stageId":"A1"},
{"count":"8","stage":"D","jCount":"8","name":"User 2","stageId":"D1"},
],

"User 3":[
{"count":"6","stage":"D","jCount":"6","name":"User 3","stageId":"D1"},
{"count":"8","stage":"B","jCount":"6","name":"User 3","stageId":"B1"},
{"count":"1","stage":"A","jCount":"6","name":"User 3","stageId":"A1"},
],
/* Many more users */
}

I am tring to change my object in this format

[
{
"name":"User 1",
"jCount":10,
"stageIdCountA1":1,
"stageIdCountB1":8,
"stageIdCountC1":0,
"stageIdCountD1":0,
},{
"name":"User 2",
"jCount":8,
"stageIdCountA1":9,
"stageIdCountB1":8,
"stageIdCountC1":7,
"stageIdCountD1":8,
},{
"name":"User 3",
"jCount":6,
"stageIdCountA1":1,
"stageIdCountB1":8,
"stageIdCountC1":0,
"stageIdCountD1":6,
},
/* Many more users */
]

There is max only 4 stages are there A1,B1,C1,D1 and jCount is common in user array of child objects

if there is no stage it should print 0

I tried to do manipulation in angularjs view but its becoming difficult.

3 Answers 3

2

You can use map to map each user object to an item in an array, and reduce inside the map function to turn the array of stages into a single object:

const input={"User 1":[{"count":"1","stage":"A","jCount":"10","name":"User 1","stageId":"A1"},{"count":"8","stage":"B","jCount":"10","name":"User 1","stageId":"B1"},],"User 2":[{"count":"7","stage":"C","jCount":"8","name":"User 2","stageId":"C1"},{"count":"8","stage":"B","jCount":"8","name":"User 2","stageId":"B1"},{"count":"9","stage":"A","jCount":"8","name":"User 2","stageId":"A1"},{"count":"8","stage":"D","jCount":"8","name":"User 2","stageId":"D1"},],"User 3":[{"count":"6","stage":"D","jCount":"6","name":"User 3","stageId":"D1"},{"count":"8","stage":"B","jCount":"6","name":"User 3","stageId":"B1"},{"count":"1","stage":"A","jCount":"6","name":"User 3","stageId":"A1"},],};

const stages = ['A', 'B', 'C', 'D'];
const output = Object.entries(input).map(([name, arr]) => {
  const { jCount } = arr[0];
  const stageCounts = stages.reduce((a, stageName) => {
    const propName = 'stageIdCount' + stageName;
    const foundStage = arr.find(({ stage }) => stageName === stage);
    const count = foundStage ? foundStage.count : 0;
    a[propName] = count;
    return a;
  }, {});
  return { name, jCount, ...stageCounts };
});
console.log(output);

If you can't use spread syntax (which you should - integrate Babel into your build process if at all possible), then replace

return { name, jCount, ...stageCounts };

with

return Object.assign({ name, jCount }, stageCounts);
Sign up to request clarification or add additional context in comments.

3 Comments

can you tell in ES2015 pls
@Mrworldwide: return Object.assign({name, jCount}, stageCounts);.
@Mrworldwide Save yourself a life of pain,.. set yourself up a toolchain that transpiles.. Having to worry about ES5, ES6, ES2015, etc etc,. Is not something I can stomach..
1

Using ES6 and Object.entries you can do something like:

const obj = {
  'User 1': [
{ count: '1', stage: 'A', jCount: '10', name: 'User 1', stageId: 'A1' },
{ count: '8', stage: 'B', jCount: '10', name: 'User 1', stageId: 'B1' }
  ],

  'User 2': [
{ count: '7', stage: 'C', jCount: '8', name: 'User 2', stageId: 'C1' },
{ count: '8', stage: 'B', jCount: '8', name: 'User 2', stageId: 'B1' },
{ count: '9', stage: 'A', jCount: '8', name: 'User 2', stageId: 'A1' },
{ count: '8', stage: 'D', jCount: '8', name: 'User 2', stageId: 'D1' }
  ],

  'User 3': [
{ count: '6', stage: 'D', jCount: '6', name: 'User 3', stageId: 'D1' },
{ count: '8', stage: 'B', jCount: '6', name: 'User 3', stageId: 'B1' },
{ count: '1', stage: 'A', jCount: '6', name: 'User 3', stageId: 'A1' }
  ]
  /* Many more users */
};

const stages = ['A1', 'B1', 'C1', 'D1'];

const getCount = (stage, user) => {
  const stageItem = obj[user.name].find(s => s.stageId === stage);
  return stageItem ? stageItem.count : 0;
};

const r = Object.entries(obj)
  .map(([name, user]) => ({ name, jCount: user[0].jCount }))
  .map(user => {
const stagesCounts = stages
  .map(stage => ({
    [`stageIdCount${stage}`]: getCount(stage, user)
  }))
  .reduce((acc, stage) => ({ ...acc, ...stage }), {});
return { ...user, ...stagesCounts };
  });

console.log(r);

Update (ES5)

const obj = {
  'User 1': [
    { count: '1', stage: 'A', jCount: '10', name: 'User 1', stageId: 'A1' },
    { count: '8', stage: 'B', jCount: '10', name: 'User 1', stageId: 'B1' }
  ],

  'User 2': [
    { count: '7', stage: 'C', jCount: '8', name: 'User 2', stageId: 'C1' },
    { count: '8', stage: 'B', jCount: '8', name: 'User 2', stageId: 'B1' },
    { count: '9', stage: 'A', jCount: '8', name: 'User 2', stageId: 'A1' },
    { count: '8', stage: 'D', jCount: '8', name: 'User 2', stageId: 'D1' }
  ],

  'User 3': [
    { count: '6', stage: 'D', jCount: '6', name: 'User 3', stageId: 'D1' },
    { count: '8', stage: 'B', jCount: '6', name: 'User 3', stageId: 'B1' },
    { count: '1', stage: 'A', jCount: '6', name: 'User 3', stageId: 'A1' }
  ]
  /* Many more users */
};

const stages = ['A1', 'B1', 'C1', 'D1'];

function getCount(stage, user) {
  const stageItem = obj[user.name].find(s => s.stageId === stage);
  return stageItem ? stageItem.count : 0;
}

function mapStages(user) {
  return stages
    .map(stage => ({
      [`stageIdCount${stage}`]: getCount(stage, user)
    }))
    .reduce((acc, stage) => ({ ...acc, ...stage }), {});
}

const r = Object.entries(obj)
  .map(function(entry) {
    return { name: entry[0], jCount: entry[1][0].jCount };
  })
  .map(function(user) {
    return Object.assign(user, mapStages(user));
  });

console.log(r);

2 Comments

is their any chance I can achieve it in ES5..?
Sure @Mrworldwide
0

This is how I found my solution:

var obj = {
	'User 1': [
		{ count: '1', stage: 'A', jCount: '10', name: 'User 1', stageId: 'A1' },
		{ count: '8', stage: 'B', jCount: '10', name: 'User 1', stageId: 'B1' }
	],
	
	'User 2': [
		{ count: '7', stage: 'C', jCount: '8', name: 'User 2', stageId: 'C1' },
		{ count: '8', stage: 'B', jCount: '8', name: 'User 2', stageId: 'B1' },
		{ count: '9', stage: 'A', jCount: '8', name: 'User 2', stageId: 'A1' },
		{ count: '8', stage: 'D', jCount: '8', name: 'User 2', stageId: 'D1' }
	],
	
	'User 3': [
		{ count: '6', stage: 'D', jCount: '6', name: 'User 3', stageId: 'D1' },
		{ count: '8', stage: 'B', jCount: '6', name: 'User 3', stageId: 'B1' },
		{ count: '1', stage: 'A', jCount: '6', name: 'User 3', stageId: 'A1' }
	]
	/* Many more users */
};

var stages = ['A1', 'B1', 'C1', 'D1'];

function getCount(stage, user) {
	var stageItem = obj[user.name].find(function (s) {
		return s.stageId === stage;
	});
	return stageItem ? stageItem.count : 0;
}

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function mapStages(user) {
	return stages.map(function (stage) {
		return _defineProperty({}, "stageIdCount" + stage, getCount(stage, user));
		}).reduce(function (acc, stage) {
		return _extends({}, acc, stage);
	}, {});
}

var r = Object.entries(obj)
.map(function(entry) {
	return { name: entry[0], jCount: entry[1][0].jCount };
})
.map(function(user) {
	return Object.assign(user, mapStages(user));
});

console.log(r);

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.