如何添加嵌套数组属性的数值
How to add the numeric values of properties of a nested array?
我正试图弄清楚如何创建一个函数,添加用户选择的元素的值,并能够通过prompt和console.log显示结果。此外,我想知道是否有一种方法可以做到这一点,即我不需要指定所选的元素,以便函数在代码中找到哪些元素被选中并执行加法函数。因为很明显,如果选项列表更长,我不想为每个潜在的选项组合创建一个新函数。顺便说一句,我想if语句也会遇到同样的问题,在这种情况下,switch语句是否是解决"DRY"代码需求的最有效方法?
我的javascript代码:请假设用户只选择嵌套数组的第一个元素。此外,"一"一词价值8美元。
var selection = new Array (3);
selection[0] = new Array ('$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8');
selection[1] = new Array ('phone1', 'phone2', 'phone3');
selection[2] = new Array ('one', 'two', 'three');
function pickPhone () {
var yourPhone = prompt("pick phone: phone1: $1, phone2: $2, phone3: $3");
if (yourPhone == selection[1][0]) {
console.log(selection[1][0] + " will cost: " + selection[0][0]);
alert(selection[1][0] + " will cost: " + selection[0][0]);
pickTerm ();
} if (yourPhone == "phone2") {
alert(selection[1][1] + " will cost: " + selection[0][1]);
} if (yourPhone == "phone3") {
alert(selection[1][2] + " will cost: " + selection[0][2]);
}
}
function pickTerm () {
var yourTerm = prompt("pick term: one, two or three?");
if (yourTerm == selection[2][0]) {
alert("Your total so far is: ??");
}
}
pickPhone ();
非常感谢您的帮助,谢谢。
保持阵列的解决方案
http://jsfiddle.net/OxyDesign/o10ezyun/
JS-
var selection = new Array(3);
selection[0] = new Array(1,2,3,4,5,6,7,8);
selection[1] = new Array('phone1', 'phone2', 'phone3');
selection[2] = new Array('one', 'two', 'three');
var firstValue;
function pickPhone() {
var yourPhone = prompt("pick phone: phone1: $1, phone2: $2, phone3: $3"),
index = selection[1].indexOf(yourPhone);
if(!~index){
pickPhone();
}else{
firstValue = selection[0][index];
alert(selection[1][index] + " will cost: $" + firstValue);
pickTerm();
}
}
function pickTerm() {
var yourTerm = prompt("pick term: one, two or three?"),
index = selection[2].indexOf(yourTerm),
finalValue = '$'+(firstValue+selection[0][index]);
if(!~index){
pickTerm();
}else{
alert("Your total so far is: "+finalValue);
}
}
pickPhone();
我不确定你到底在解决什么问题。这些清单有多长(电话、费用等)?为这些项目设置了什么类型的映射?
现在,我建议在这样的对象中合并相应的值:
// item -> cost
var phones = [
{title: 'phone1', cost: '$1'},
{title: 'phone2', cost: '$2'},
{title: 'phone3', cost: '$3'}
],
terms = [
{title: 'one', cost: '$8'},
{title: 'two', cost: '$2'},
{title: 'three', cost: '$3'}
],
phonesListWithCosts = (function(list) {
return list.reduce(function(memo, item) {
return memo + item.title + ': ' + item.cost;
}, '');
}(phones)),
termsList = (function(list) {
return list.reduce(function(memo, item) {
return memo + ', ' + item.title;
}, '');
}(terms)),
findBy = function(array, property, value) {
return array.filter(function(item) {
return item[property] === value;
})[0];
},
getItem = function(list, promptMessage) {
var selectedItemTitle = prompt(promptMessage);
return findBy(list, 'title', selectedItemTitle);
},
getItemCost = function(item) {
return parseInt(item.cost.replace(/'D/g, ''), 10);
},
pickPhone = function() {
var selectedPhone = getItem(phones, 'pick phone: ' + phonesListWithCosts),
firstPhone = phones[0],
message;
if (selectedPhone) {
message = [selectedPhone.title, 'will cost:', selectedPhone.cost].join(' ');
console.log(message);
alert(message);
if (selectedPhone === firstPhone) {
pickTerm(getItemCost(selectedPhone));
}
}
},
pickTerm = function(accumCost) {
var selectedTerm = getItem(terms, 'pick term: ' + termsList),
totalCost,
message;
if (selectedTerm) {
totalCost = accumCost + getItemCost(selectedTerm);
message = 'Your total so far is: $' + totalCost;
alert(message);
}
};
pickPhone();
jsbin演示。
相关文章:
- 如何在不链接/jquery的情况下使用方法应用css属性数组
- 如何使用lodash返回与模式匹配的属性数组
- 从嵌套属性数组中获取对象嵌套值
- 如何使用 javascript 返回数据属性数组
- JQuery 获取返回的属性数组
- 对公共属性数组进行排序
- Angularjs-ng repeat不反映属性数组中的更改
- JADE&NodeJS访问JSON对象属性数组
- Lodash 按数组的属性数组过滤
- 将对象数组转换为属性数组
- 如何从一个共享属性的jQuery集合中获取一个属性数组
- 骨干移除和新视图不会重置其属性数组内容
- 使用JSON从对象数组创建属性数组
- 将数据属性数组转换为对象
- 从属性数组和匹配属性值数组创建JavaScript对象的有效方法
- 访问json中的属性数组
- jquery.获得没有重复的自定义属性数组
- 如何更新对象属性数组值
- 多属性数组语法
- jQuery:生成"name”;动态属性数组