为了账号安全,请及时绑定邮箱和手机立即绑定

js排序的问题

js排序的问题

var list = [    {products:products0,price:11,amount:10,profit:5},    {products:products1,price:13,amount:55,profit:6},    {products:products2,price:23,amount:105,profit:12},    {products:products3,price:44,amount:40,profit:25},    {products:products4,price:52,amount:20,profit:35},    {products:products5,price:10,amount:66,profit:2},    {products:products6,price:44,amount:34,profit:25},    {products:products7,price:77,amount:77,profit:45}];问题1 将上面的数组改写为 list1 ={    products:[products0,products1,...],    price:[11,13,...],    amount:[10,55,...],    profit:[5,6,12,...]}用js实现。问题2 获取其中最高和最低单价的产品;问题3 按数量从高到低进行排序;问题4 获取最大和最小总利润的产品;
查看完整描述

3 回答

?
千秋此意

TA贡献158条经验 获得超187个赞

'use strict'

var list = [
    { products: 'products0', price: 11, amount: 10, profit: 5 },
    { products: 'products1', price: 13, amount: 55, profit: 6 },
    { products: 'products2', price: 23, amount: 105, profit: 12 },
    { products: 'products3', price: 44, amount: 40, profit: 25 },
    { products: 'products4', price: 52, amount: 20, profit: 35 },
    { products: 'products5', price: 10, amount: 66, profit: 2 },
    { products: 'products6', price: 44, amount: 34, profit: 25 },
    { products: 'products7', price: 77, amount: 77, profit: 45 }
];

Array.prototype.sortby = function(key, flag) { // 此方法只适用于当前这个问题
    return this.slice(0).sort((a, b) => flag ? b[key] - a[key] : a[key] - b[key]);
}

// 问题1: 将上面的数组改写为:
var list1 = function() {
    var tempObj = {};
    for (let i = 0; i < list.length; i++) {
        let item = list[i];
        for (let key in item) {
            if (!tempObj[key]) { tempObj[key] = [] }
            if (!!tempObj[key] && tempObj[key] instanceof Array) { tempObj[key].push(item[key]) }
        }
    }
    return tempObj;
}();

console.log('\r将上面的数组改写为: \r', list1);

// 问题2 获取其中最高和最低单价的产品
var list2 = list.sortby('price');
var products2 = {
    lowestPrice: list2[0].products,
    highestPrice: list2[list2.length - 1].products
}
console.log('\r获取其中最高和最低单价的产品: ', products2);

// 问题3 按数量从高到低进行排序;
var list3 = list.sortby('amount', true);
console.log('\r按数量从高到低进行排序: \r', list3);

// 问题4 获取最大和最小总利润的产品;
var list4 = list.sort((a, b) => {
    return a['amount'] * a['profit'] - b['amount'] * b['profit'];
});

var products4 = {
    minTotalProfit: list4[0].products,
    maxTotalProfit: list4[list4.length - 1].products
}

console.log('\r获取最大和最小总利润的产品;', products4);

/** ===================最终输出结果如下===================
将上面的数组改写为: 
 { products: 
   [ 'products0',
     'products1',
     'products2',
     'products3',
     'products4',
     'products5',
     'products6',
     'products7' ],
  price: [ 11, 13, 23, 44, 52, 10, 44, 77 ],
  amount: [ 10, 55, 105, 40, 20, 66, 34, 77 ],
  profit: [ 5, 6, 12, 25, 35, 2, 25, 45 ] }

获取其中最高和最低单价的产品:  { lowestPrice: 'products5', highestPrice: 'products7' }

按数量从高到低进行排序: 
 [ { products: 'products2', price: 23, amount: 105, profit: 12 },
  { products: 'products7', price: 77, amount: 77, profit: 45 },
  { products: 'products5', price: 10, amount: 66, profit: 2 },
  { products: 'products1', price: 13, amount: 55, profit: 6 },
  { products: 'products3', price: 44, amount: 40, profit: 25 },
  { products: 'products6', price: 44, amount: 34, profit: 25 },
  { products: 'products4', price: 52, amount: 20, profit: 35 },
  { products: 'products0', price: 11, amount: 10, profit: 5 } ]

获取最大和最小总利润的产品; { minTotalProfit: 'products0', maxTotalProfit: 'products7' }
=============================================== **/


查看完整回答
4 反对 回复 2017-03-28
?
西兰花伟大炮

TA贡献376条经验 获得超318个赞

var proarr = [];
            for (var j = 0;j < len; j++) {
                proarr.push(list1.amount[j]*list1.profit[j]);
            }
            console.log(proarr);

第四题,这个写完跟第二题是一样的,多了个乘起来,欢迎采纳

查看完整回答
1 反对 回复 2017-03-28
  • 千秋此意
    千秋此意
    咱俩又撞车了=。=
  • 西兰花伟大炮
    西兰花伟大炮
    老哥,让我一回呗
  • 千秋此意
    千秋此意
    哈哈,其实采纳与否我不怎么在意的,我是把问题直接拷到编辑器去写,写的时候还没人回答,写完才发现你已经回答了~只能说巧合啊
点击展开后面2
?
西兰花伟大炮

TA贡献376条经验 获得超318个赞

var sortarr = list1.amount.sort(function(a,b){
                return a - b; 
            })
            console.log(sortarr);

第三题

查看完整回答
1 反对 回复 2017-03-28
?
西兰花伟大炮

TA贡献376条经验 获得超318个赞

var len = list1.products.length;
            var maxpro = Math.max.apply(null,list1.price);
            //console.log(maxpro);
            var minpro = Math.min.apply(null,list1.price);
            for (var k = 0;k < len; k++) {
                if(list1.price[k] == maxpro){
                    console.log("max: " + list1.products[k]);
                }else if(list1.price[k] == minpro){
                    console.log("min: " + list1.products[k]);
                }
            }

第二题

查看完整回答
1 反对 回复 2017-03-28
?
西兰花伟大炮

TA贡献376条经验 获得超318个赞

var list = [
                   {products:"products0",price:11,amount:10,profit:5},
                   {products:"products1",price:13,amount:55,profit:6},
                   {products:"products2",price:23,amount:105,profit:12},
                   {products:"products3",price:44,amount:40,profit:25},
                   {products:"products4",price:52,amount:20,profit:35},
                   {products:"products5",price:10,amount:66,profit:2},
                   {products:"products6",price:44,amount:34,profit:25},
                   {products:"products7",price:77,amount:77,profit:45}
                ];
            var list1 = {
                products:[],
                price:[],
                amount:[],
                profit:[]
            };
            for (var i = 0;i < list.length;i++) {
                list1["products"].push(list[i].products);
                list1["price"].push(list[i].price);
                list1["amount"].push(list[i].amount);
                list1["profit"].push(list[i].profit);
            }
            console.log(list1.products);
            console.log(list1.price);
            console.log(list1.amount);
            console.log(list1.profit);

第一题

查看完整回答
1 反对 回复 2017-03-28
  • 3 回答
  • 1 关注
  • 1683 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信