Mam taki obiekt

{
"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 */
}

Próbuję zmienić swój obiekt w tym formacie

[
{
"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 */
]

Istnieje max tylko 4 etapy, są tam A1, B1, C1, D1 i jcount jest powszechny w tablicach użytkownika obiektów podrzędnych

Jeśli jest bez etapu , powinien drukować 0

Próbowałem zrobić manipulację w widoku Angularjs, ale staje się trudne.

4
Mr world wide 2 czerwiec 2018, 03:46

3 odpowiedzi

Najlepsza odpowiedź

Korzystanie z ES6 i {X0}} możesz zrobić coś takiego:

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

Aktualizacja (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);
1
J. Pichardo 2 czerwiec 2018, 01:46

Możesz użyć map, aby mapować każdy obiekt użytkownika do elementu w tablicy, a {x1}} wewnątrz funkcji mapy, aby obrócić tablicę etapów do jednego obiektu:

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

Jeśli nie możesz użyć składni rozprzestrzeniania (co powinieneś - zintegrować Babel w procesie budowy, jeśli jest to możliwe), a następnie wymień

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

Z

return Object.assign({ name, jCount }, stageCounts);
2
CertainPerformance 2 czerwiec 2018, 01:04

W ten sposób znalazłem moje rozwiązanie:

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);
0
Mr world wide 2 czerwiec 2018, 02:02