首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript -基于三个数组组成一个数组的函数(合并)

JavaScript -基于三个数组组成一个数组的函数(合并)
EN

Stack Overflow用户
提问于 2019-06-19 08:51:15
回答 1查看 57关注 0票数 1

给定以下js数组:

代码语言:javascript
复制
var prices_A =[
['company','100g','200g','300g'],
['business_a','40€','50€',''],
['business_b','50€','61€','42€'],
['business_c','66€','','31€']
]

var prices_B =[
['company','100g','200g','300g'],
['business_b','40€','50€',''],
['business_d','50€','61€','42€'],
['business_e','66€','','31€']
]

var prices_C =[
['company','100g','200g','300g'],
['business_e','40€','50€',''],
['business_b','66€','','31€']
]

如何使函数传递两个变量:gram和price_type,返回基于输入的数组合并(变量传递)。price_type可以是: prices_A、prices_B、prices_C或prices_all。当引入prices_all时,它应该查询合并所有价格(prices_A、prices_B和prices_C)

示例1:

代码语言:javascript
复制
var prices = myfunction(200,prices_C){}

结果:

代码语言:javascript
复制
prices =[
['company','200g'],
['business_e','50€']
]

示例2:

代码语言:javascript
复制
var prices = myfunction(100,prices_all){}

结果:

代码语言:javascript
复制
prices =[
['company','100g_A','100g_B','100g_C'],
['business_a','40€',''],
['business_b','50€','40€','66€'],
['business_c','66€',''],
['business_d','','50€',''],
['business_e','','66€','40€'],
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 09:41:39

这是一个解决方案,它为一个组(如果给定)或所有组获得一个价格,然后它将这些组分配给一个数组。

代码语言:javascript
复制
function getPrices(weight, category) {

    function getByGroup(group) {
        var index = group[0].indexOf(weight);
        return group
            .map(a => [0, index].map(i => a[i]))
            .filter(a => a[1]);
    }

    return category in allPrices
        ? getByGroup(allPrices[category])
        : Object.entries(allPrices).reduce((r, [k, group], i) => {
            var temp = getByGroup(group),
                index;

            if (!temp.length) return r;
            temp[0][1] += '_' + k.slice(-1);
            if (!r) return temp;
            index = r[0].push(temp[0][1]) - 1;            
            temp.forEach(([company, price]) => {
                var row = r.find(([c]) => c === company);
                if (!row) {
                    r.push(row = [company]);
                } 
                row[index] = price;
            });
            r.forEach(a => a[index] = a[index] || '');
            return r;
        }, undefined);
}


var prices_A = [['company', '100g', '200g', '300g'], ['business_a', '40€', '50€', ''], ['business_b', '50€', '61€', '42€'], ['business_c', '66€', '', '31€']],
    prices_B = [['company', '100g', '200g', '300g'], ['business_b', '40€', '50€', ''], ['business_d', '50€', '61€', '42€'], ['business_e', '66€', '', '31€']],
    prices_C = [['company', '100g', '200g', '300g'], ['business_e', '40€', '50€', ''], ['business_b', '66€', '', '31€']],
    allPrices = { prices_A, prices_B, prices_C };

console.log(getPrices('200g', 'prices_C'));
console.log(getPrices('100g', 'prices_all'));
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56663687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档