如何添加嵌套数组属性的数值

How to add the numeric values of properties of a nested array?

本文关键字:属性 数组 嵌套 何添加 添加      更新时间:2023-09-26

我正试图弄清楚如何创建一个函数,添加用户选择的元素的值,并能够通过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演示。