我有一个这样的对象
{
"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 */
}
我正在尝试更改此格式的对象
[
{
"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 */
]
最多只有4个阶段A1、B1、C1、D1和jCount在子对象的用户数组中很常见
如果没有stage,则应打印
我试着在angularjs视图中进行操作,但它变得很困难。
发布于 2018-06-02 09:25:36
使用ES6和Object.entries
,您可以执行以下操作:
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);
更新(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);
发布于 2018-06-02 08:54:01
您可以使用map
将每个用户对象映射到数组中的项目,并在map函数中使用reduce
将阶段数组转换为单个对象:
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);
如果您不能使用扩展语法(您应该尽可能将Babel集成到您的构建过程中),请替换
return { name, jCount, ...stageCounts };
使用
return Object.assign({ name, jCount }, stageCounts);
发布于 2018-06-02 10:02:47
这是我如何找到我的解决方案的:
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);
https://stackoverflow.com/questions/50652548
复制相似问题