我創建了一個函數,它將返回一個沒有重複分支的對象和每個分支的總銷售額。獲得總銷售額
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]);
}
任何人誰知道我的錯誤,我該如何解決它?
謝謝!
你好,我想了解你的答案的一部分,因爲我軟弱與對象。對於這一行,「myObj [splitTeam] = myObj [splitTeam]?myObj [splitTeam]:0;」 ;這是相當於「if(myObj [splitTeam])返回myObj [splitTeam]:0是嗎?它只是一個表達式被返回?我習慣於看到2個表達式與三元運算符 –
this'myObj [splitTeam] = myObj [splitTeam]?myObj [splitTeam]:0;'等於'if(myObj [splitTeam]){myObj [splitTeam] = myObj [splitTeam]} else {myObj [splitTeam] = 0}我們在第一次添加一個對象時,這個值將會被取消查找,所以我將它設置爲0,如果值存在,然後再加上那個值 – Durga
另外還有一件事,對象知道自己跳過重複嗎?數組中,他有對象具有重複的屬性。對象只是略過,只有獨特的屬性?感謝alp的解釋。 –