Javascript AngularJS:检查对象中是否存在键/值,并构造一个对象数组
这是我正在使用的一种服务方法。它查找所有项目收入(与此问题相关:),并生成一个新的对象数组 我希望该对象数组能够按年度和三个月对收入进行排序,这样我就可以在视图中轻松地循环到该数组中(使用ng repeat)。 我的当前数据(由Javascript AngularJS:检查对象中是否存在键/值,并构造一个对象数组,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,这是我正在使用的一种服务方法。它查找所有项目收入(与此问题相关:),并生成一个新的对象数组 我希望该对象数组能够按年度和三个月对收入进行排序,这样我就可以在视图中轻松地循环到该数组中(使用ng repeat)。 我的当前数据(由incomers.buildandGetIncomers()返回,您可以看到以下内容): 我想要的数据结构是: [ { year: 2014, trim: [ { name :
incomers.buildandGetIncomers()
返回,您可以看到以下内容):
我想要的数据结构是:
[
{
year: 2014,
trim: [
{
name : 'first',
content : [
// some content
]
},
{
name : 'second',
content : [
// some content
]
}
]
},
{
year: 2013,
trim: [
{
name : 'first',
content : [
// some content
]
}
]
}
]
下面是我现在使用的方法:
self.trimestral = function(){
var deferred = $q.defer();
self.buildAndGetIncomes().then(function(result) {
var trimestral = [];
var incomes = result;
angular.forEach(incomes, function(income, i){
var year = income.year,
trim = income.trim,
newTrim = {},
newTrimContent = {};
if( i === 0 ){
newTrim.year = year;
newTrim.trim = [];
newTrimContent ={};
newTrimContent.name = trim;
newTrimContent.content = [];
newTrimContent.content.push(income);
trimestral.push(newTrim);
console.log(trimestral);
trimestral[0].trim.push(newTrimContent);
}
else {
var maxLength = incomes.length;
for (var h = 0; h<maxLength; h++){
if(trimestral[h].year === year){
for (var j = 0; j<trimestral[h].trim.length; j++){
console.log(h,j,trimestral[h].trim[j].name === trim);
if(trimestral[h].trim[j].name === trim){ // trimester already exists
trimestral[h].trim[j].content.push(income);
}
else {// trimester doesn't exist, create it
var createTrim = {};
createTrim.name = trim;
createTrim.content = [];
createTrim.content.push(income);
trimestral[h].trim.push(newTrimContent);
}
}
}
else {
newTrim.year = year;
newTrim.trim = [];
newTrimContent ={};
newTrimContent.name = trim;
newTrimContent.content = [];
newTrimContent.content.push(income);
trimestral.push(newTrim);
console.log(trimestral);
trimestral[0].trim.push(newTrimContent);
}
}
}
});
deferred.resolve(trimestral);
});
return deferred.promise;
};
self.trimestral=function(){
var deferred=$q.deferred();
self.buildandGetIncomers().then(函数(结果){
var trimestral=[];
var收益=结果;
收入,功能(收入,i){
var年=收入年,
trim=income.trim,
newTrim={},
newTrimContent={};
如果(i==0){
newTrim.year=年;
newTrim.trim=[];
newTrimContent={};
newTrimContent.name=修剪;
newTrimContent.content=[];
newTrimContent.content.push(收入);
微调器推压(newTrim);
控制台日志(trimestral);
trimestral[0].trim.push(newTrimContent);
}
否则{
var maxLength=收益。长度;
对于(var h=0;h<p>),这可能是使用几个循环,但代码将很难遵循。如果你可以带上另一个库,考虑下划线。JS。用它,你可以做这样的事情……
var trimestral = _(incomes).chain()
.groupBy('year')
.map(function (items, year) {
return {
year: year,
trim: _(items).chain()
.groupBy('trim')
.map(function(content, trimester) {
return {
trim: trimester,
content: content
};
})
.sortBy(function(i) {
var trimesters = {
first: 1,
second: 2,
third: 3
};
return trimesters[i.trim];
}).value()
};
})
.sortBy('year')
.value();
我最后请一位优秀的web开发人员朋友帮我做这件事。他告诉我分两步来做:构造对象,然后按照我需要的方式格式化它
所以我这样做了:
self.trimestral = function(){
var deferred = $q.defer();
var global = [];
self.buildAndGetIncomes().then(function(result) {
var trimestral = {};
var incomes = result;
// First loop, construct the object
angular.forEach(incomes, function(income, i){
var year = income.year,
trim = income.trim,
newTrim = {},
newTrimContent = {};
if(trimestral[year] === undefined){
trimestral['year' , year] = {};
}
if(trimestral[year][trim] === undefined) {
trimestral[year][trim] = [];
}
trimestral[year][trim].push(income);
});
deferred.resolve(global);
// Second loop, format the data
for ( prop in trimestral ) {
newYear = {};
newYear.name = prop;
newYear.content = [];
for ( trim in trimestral[prop]){
newTrim = {};
newTrim.name = trim;
newTrim.content = [];
newTrim.content = trimestral[prop][trim];
newYear.content.push(newTrim);
}
global.push(newYear);
}
});
return deferred.promise;
};
我想它可能更干净,但它可以工作,而且比我预期的简单得多。您是否尝试过使用orderBy
过滤器来执行排序,无论是在ngRepeat
s中还是在您的控制器中?我可以在上面粘贴的数组上使用orderBy,但我无法构建它。这是我面临的问题。您能st当前的数据结构和您想要的最终结果?当然,我很遗憾我以前没有发布它。我发布了它。我想我的问题只是关于纯JS循环和内部循环。但正如您所看到的,我正在陷入其中…我想了想,我已经知道我可以使用jQuery$.map()但是我不想使用另一个js库:)谢谢你!
self.trimestral = function(){
var deferred = $q.defer();
var global = [];
self.buildAndGetIncomes().then(function(result) {
var trimestral = {};
var incomes = result;
// First loop, construct the object
angular.forEach(incomes, function(income, i){
var year = income.year,
trim = income.trim,
newTrim = {},
newTrimContent = {};
if(trimestral[year] === undefined){
trimestral['year' , year] = {};
}
if(trimestral[year][trim] === undefined) {
trimestral[year][trim] = [];
}
trimestral[year][trim].push(income);
});
deferred.resolve(global);
// Second loop, format the data
for ( prop in trimestral ) {
newYear = {};
newYear.name = prop;
newYear.content = [];
for ( trim in trimestral[prop]){
newTrim = {};
newTrim.name = trim;
newTrim.content = [];
newTrim.content = trimestral[prop][trim];
newYear.content.push(newTrim);
}
global.push(newYear);
}
});
return deferred.promise;
};