我创建了一个函数,它将返回一个没有重复分支的对象和每个分支的总销售额。
var salesPersons = [
{"name": {"first": "Aleen", "last" :"Atkins"}, "sales" : "$2314", "branch": "Austin, TX"},
{"name": {"first": "Alvaro", "last" :"Angelos"}, "sales" : "$1668", "branch": "New York City, NY"},
{"name": {"first": "Denese", "last" :"Dossett"}, "sales" : "$9248", "branch": "Austin, TX"},
{"name": {"first": "Douglas", "last" :"Denney"}, "sales" : "$5058", "branch": "New York City, NY"},
{"name": {"first": "Earline", "last" :"Erickson"}, "sales" : "$18876", "branch": "New York City, NY"},
{"name": {"first": "Herman", "last" :"Hazell"}, "sales" : "$2746", "branch": "New York City, NY"},
{"name": {"first": "Homer", "last" :"Hirth"}, "sales" : "$474", "branch": "Orlando, FL"},
{"name": {"first": "Hwa", "last" :"Heidt"}, "sales" : "$9607", "branch": "San Francisco, CA"},
{"name": {"first": "Hyon", "last" :"Hampshire"}, "sales" : "$13598", "branch": "New York City, NY"},
{"name": {"first": "Issac", "last" :"Ingerson"}, "sales" : "$5225", "branch": "Austin, TX"},
{"name": {"first": "Jeraldine", "last" :"Joplin"}, "sales" : "$2891", "branch": "New York City, NY"},
{"name": {"first": "Jin", "last" :"Jeffrey"}, "sales" : "$14402", "branch": "Austin, TX"},
{"name": {"first": "Joleen", "last" :"Jolin"}, "sales" : "$15736", "branch": "Austin, TX"},
{"name": {"first": "Jude", "last" :"Jarrett"}, "sales" : "$7557", "branch": "San Francisco, CA"},
{"name": {"first": "Magda", "last" :"Mireles"}, "sales" : "$1498", "branch": "Austin, TX"},
{"name": {"first": "Mistie", "last" :"Montealegre"}, "sales" : "$6920", "branch": "New York City, NY"},
{"name": {"first": "Nancy", "last" :"Napoli"}, "sales" : "$5255", "branch": "New York City, NY"},
{"name": {"first": "Regine", "last" :"Rohrbaugh"}, "sales" : "$7881", "branch": "Orlando, FL"},
{"name": {"first": "Rolando", "last" :"Riebel"}, "sales" : "$8573", "branch": "San Francisco, CA"},
{"name": {"first": "Scarlett", "last" :"Stagg"}, "sales" : "$7126", "branch": "Orlando, FL"},
{"name": {"first": "Sherron", "last" :"Strawn"}, "sales" : "$8848", "branch": "New York City, NY"},
{"name": {"first": "Susan", "last" :"Shilling"}, "sales" : "$8542", "branch": "Seattle, WA"},
{"name": {"first": "Tama", "last" :"Tworek"}, "sales" : "$9200", "branch": "Seattle, WA"},
{"name": {"first": "Tonisha", "last" :"Taunton"}, "sales" : "$5219", "branch": "Orlando, FL"},
{"name": {"first": "Vergie", "last" :"Villescas"}, "sales" : "$8712", "branch": "New York City, NY"}
];
function convertSalesToInt(person){
var salesInt = person.sales.slice(1);
return parseInt(salesInt, 10);
}
function salesPerBranch(salesPersons){
var myObj = {};
var uniq = [];
var total = 0;
for(var i = 0; i < salesPersons.length; i++){
var splitTeam = salesPersons[i].branch;
if(uniq.indexOf(salesPersons) < 0){
myObj[salesPersons] = total+= convertSalesToInt(salesPersons[i]);
}
}
return myObj;
}
console.log(salesPerBranch(salesPersons));
/* { 'Austin, TX': 110898,
'New York City, NY': 187174,
'Orlando, FL': 178462,
'San Francisco, CA': 139527,
'Seattle, WA': 173243 } */
答案应如下所示:
{
'Austin, TX': 48423,
'New York City, NY': 74572,
'Orlando, FL': 20700,
'San Francisco, CA': 25737,
'Seattle, WA': 17742
}
我现在被困住的唯一部分是我为每个分支添加总销售额的部分。现在它没有提高适量。这部分负责:
if(uniq.indexOf(salesPersons) < 0){
myObj[salesPersons] = total+= convertSalesToInt(salesPersons[i]);
}
任何知道我的错误以及如何解决错误的人?
谢谢!
答案 0 :(得分:3)
var salesPersons = [
{"name": {"first": "Aleen", "last" :"Atkins"}, "sales" : "$2314", "branch": "Austin, TX"},
{"name": {"first": "Alvaro", "last" :"Angelos"}, "sales" : "$1668", "branch": "New York City, NY"},
{"name": {"first": "Denese", "last" :"Dossett"}, "sales" : "$9248", "branch": "Austin, TX"},
{"name": {"first": "Douglas", "last" :"Denney"}, "sales" : "$5058", "branch": "New York City, NY"},
{"name": {"first": "Earline", "last" :"Erickson"}, "sales" : "$18876", "branch": "New York City, NY"},
{"name": {"first": "Herman", "last" :"Hazell"}, "sales" : "$2746", "branch": "New York City, NY"},
{"name": {"first": "Homer", "last" :"Hirth"}, "sales" : "$474", "branch": "Orlando, FL"},
{"name": {"first": "Hwa", "last" :"Heidt"}, "sales" : "$9607", "branch": "San Francisco, CA"},
{"name": {"first": "Hyon", "last" :"Hampshire"}, "sales" : "$13598", "branch": "New York City, NY"},
{"name": {"first": "Issac", "last" :"Ingerson"}, "sales" : "$5225", "branch": "Austin, TX"},
{"name": {"first": "Jeraldine", "last" :"Joplin"}, "sales" : "$2891", "branch": "New York City, NY"},
{"name": {"first": "Jin", "last" :"Jeffrey"}, "sales" : "$14402", "branch": "Austin, TX"},
{"name": {"first": "Joleen", "last" :"Jolin"}, "sales" : "$15736", "branch": "Austin, TX"},
{"name": {"first": "Jude", "last" :"Jarrett"}, "sales" : "$7557", "branch": "San Francisco, CA"},
{"name": {"first": "Magda", "last" :"Mireles"}, "sales" : "$1498", "branch": "Austin, TX"},
{"name": {"first": "Mistie", "last" :"Montealegre"}, "sales" : "$6920", "branch": "New York City, NY"},
{"name": {"first": "Nancy", "last" :"Napoli"}, "sales" : "$5255", "branch": "New York City, NY"},
{"name": {"first": "Regine", "last" :"Rohrbaugh"}, "sales" : "$7881", "branch": "Orlando, FL"},
{"name": {"first": "Rolando", "last" :"Riebel"}, "sales" : "$8573", "branch": "San Francisco, CA"},
{"name": {"first": "Scarlett", "last" :"Stagg"}, "sales" : "$7126", "branch": "Orlando, FL"},
{"name": {"first": "Sherron", "last" :"Strawn"}, "sales" : "$8848", "branch": "New York City, NY"},
{"name": {"first": "Susan", "last" :"Shilling"}, "sales" : "$8542", "branch": "Seattle, WA"},
{"name": {"first": "Tama", "last" :"Tworek"}, "sales" : "$9200", "branch": "Seattle, WA"},
{"name": {"first": "Tonisha", "last" :"Taunton"}, "sales" : "$5219", "branch": "Orlando, FL"},
{"name": {"first": "Vergie", "last" :"Villescas"}, "sales" : "$8712", "branch": "New York City, NY"}
];
function convertSalesToInt(person){
var salesInt = person.sales.slice(1);
return parseInt(salesInt, 10);
}
function salesPerBranch(salesPersons){
var myObj = {};
for(var i = 0; i < salesPersons.length; i++){
var splitTeam = salesPersons[i].branch;
myObj[splitTeam] = myObj[splitTeam] ? myObj[splitTeam]: 0;
myObj[splitTeam] += convertSalesToInt(salesPersons[i]);
}
return myObj;
}
console.log(salesPerBranch(salesPersons));
&#13;
如果您已经添加了销售计数数据,则需要检查是否有销售计数数据。
答案 1 :(得分:1)
这是另一个受到@ Durga答案启发的略有不同的方式。我使用for ... of循环和简化的if语句来理解他的答案。
function salesPerBranch(salesPersons) {
var myObj = {};
//loop through sales people
for (let person of salesPersons) {
let branch = person.branch;
//test for branches
if (!myObj[branch]) {
//create branches in the obj
myObj[branch] = myObj[branch]
//set default value to 0
myObj[branch] = 0;
}
//Add sales for each branch
myObj[branch] += convertSalesToInt(person);
}
console.log(myObj)
}
salesPerBranch(salesPersons);
答案 2 :(得分:0)
这应该可以解决问题
salesPersons.map(function(x){return x.branch}).filter(function(value, index, self){return self.indexOf(value) === index;}).forEach(function(x){
console.log(x + ' : $' + salesPersons.filter(function(y){if(y.branch === x) return y}).map(function(z){return z.sales.slice(1)}).reduce(function(a,b){return +a + +b;},0));
});