有人可以帮我使用 Lodash/Underscore/Javascript/jQuery 吗?
Can somebody help me using Lodash/Underscore/Javascript/jQuery?
源数据:
{
[
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
...
]
}
目标数据:
{
[
{ ItemA: 1, codeA: 'A01', nameA: 'nameA01', ItemB: 2, codeB: 'A02', nameB: 'nameA02', ItemC: 3, codeC: 'A03', nameC: 'nameA03'},
{ ItemA: 4, codeA: 'A04', nameA: 'nameA04', ItemB: 5, codeB: 'A05', nameB: 'nameA05', ItemC: 6, codeC: 'A06', nameC: 'nameA06'},
{ ItemA: 7, codeA: 'A07', nameA: 'nameA07', ItemB: 8, codeB: 'A08', nameB: 'nameA08', ItemC: 9, codeC: 'A09', nameC: 'nameA09'},
{ ItemA: 10, codeA: 'A13', nameA: 'nameA13', ...}
]
}
我前几天发的,不太实用。
有人可以帮助我使用 Lodash/Underscore/Javascript/jQuery 操作从 'Source data' 到 'Target data' 的数据吗?
非常感谢。
有趣的问题 :d。试试这个(代码尚未优化):
var a =
[
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
];
var result = _.reduce(a, function(memo, item, index) {
var mod = index % 3;
if (mod === 0) {
var newObj = {
ItemA: index + 1,
codeA: item.code,
nameA: item.name
};
memo.push(newObj);
return memo;
} else if (mod === 1) {
_.extend(memo[memo.length - 1], {
ItemB: index + 1,
codeB: item.code,
nameB: item.name
});
return memo;
} else if (mod === 2) {
_.extend(memo[memo.length - 1], {
ItemC: index + 1,
codeC: item.code,
nameC: item.name
});
return memo;
}
}, []);
console.log(result);
var src = [
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
];
var getItemNum = function(item) {
return +item.code.replace(/\w+(\d{2})/, '');
}
var groupBy3 = function(item) {
return Math.floor((getItemNum(item) - 1) / 3);
}
var dst = _.map(_.groupBy(src, groupBy3), function(items){
var res = {}
var letters = ['A', 'B', 'C'];
for (var i = 0; i < items.length; i++) {
res['Item' + letters[i]] = getItemNum(items[i]);
res['code' + letters[i]] = items[i].code;
res['name' + letters[i]] = items[i].name;
}
return res;
});
http://jsfiddle.net/onlite/mzcq4Lcd/
更新:
优化版本:
var src = [
{ code: 'A01', name: 'nameA01'},
//...
];
var getItemNum = function(item) {
return +item.code.replace(/\w+(\d{2})/, '');
}
var letters = ['A', 'B', 'C'];
var dst = _.reduce(src, function(res, item, i){
var groupIndex = Math.floor(i / 3);
var letter = letters[i % 3];
var group = res[groupIndex] || (res[groupIndex] = {});
group['Item' + letter] = getItemNum(item);
group['code' + letter] = item.code;
group['name' + letter] = item.name;
return res;
}, []);
源数据:
{
[
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
...
]
}
目标数据:
{
[
{ ItemA: 1, codeA: 'A01', nameA: 'nameA01', ItemB: 2, codeB: 'A02', nameB: 'nameA02', ItemC: 3, codeC: 'A03', nameC: 'nameA03'},
{ ItemA: 4, codeA: 'A04', nameA: 'nameA04', ItemB: 5, codeB: 'A05', nameB: 'nameA05', ItemC: 6, codeC: 'A06', nameC: 'nameA06'},
{ ItemA: 7, codeA: 'A07', nameA: 'nameA07', ItemB: 8, codeB: 'A08', nameB: 'nameA08', ItemC: 9, codeC: 'A09', nameC: 'nameA09'},
{ ItemA: 10, codeA: 'A13', nameA: 'nameA13', ...}
]
}
我前几天发的,不太实用。 有人可以帮助我使用 Lodash/Underscore/Javascript/jQuery 操作从 'Source data' 到 'Target data' 的数据吗?
非常感谢。
有趣的问题 :d。试试这个(代码尚未优化):
var a =
[
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
];
var result = _.reduce(a, function(memo, item, index) {
var mod = index % 3;
if (mod === 0) {
var newObj = {
ItemA: index + 1,
codeA: item.code,
nameA: item.name
};
memo.push(newObj);
return memo;
} else if (mod === 1) {
_.extend(memo[memo.length - 1], {
ItemB: index + 1,
codeB: item.code,
nameB: item.name
});
return memo;
} else if (mod === 2) {
_.extend(memo[memo.length - 1], {
ItemC: index + 1,
codeC: item.code,
nameC: item.name
});
return memo;
}
}, []);
console.log(result);
var src = [
{ code: 'A01', name: 'nameA01'},
{ code: 'A02', name: 'nameA02'},
{ code: 'A03', name: 'nameA03'},
{ code: 'A04', name: 'nameA04'},
{ code: 'A05', name: 'nameA05'},
{ code: 'A06', name: 'nameA06'},
{ code: 'A07', name: 'nameA07'},
{ code: 'A08', name: 'nameA08'},
{ code: 'A09', name: 'nameA09'},
{ code: 'A10', name: 'nameA10'},
{ code: 'A11', name: 'nameA11'},
{ code: 'A12', name: 'nameA12'},
{ code: 'A13', name: 'nameA13'}
];
var getItemNum = function(item) {
return +item.code.replace(/\w+(\d{2})/, '');
}
var groupBy3 = function(item) {
return Math.floor((getItemNum(item) - 1) / 3);
}
var dst = _.map(_.groupBy(src, groupBy3), function(items){
var res = {}
var letters = ['A', 'B', 'C'];
for (var i = 0; i < items.length; i++) {
res['Item' + letters[i]] = getItemNum(items[i]);
res['code' + letters[i]] = items[i].code;
res['name' + letters[i]] = items[i].name;
}
return res;
});
http://jsfiddle.net/onlite/mzcq4Lcd/
更新:
优化版本:
var src = [
{ code: 'A01', name: 'nameA01'},
//...
];
var getItemNum = function(item) {
return +item.code.replace(/\w+(\d{2})/, '');
}
var letters = ['A', 'B', 'C'];
var dst = _.reduce(src, function(res, item, i){
var groupIndex = Math.floor(i / 3);
var letter = letters[i % 3];
var group = res[groupIndex] || (res[groupIndex] = {});
group['Item' + letter] = getItemNum(item);
group['code' + letter] = item.code;
group['name' + letter] = item.name;
return res;
}, []);