千秋此意
'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' }
=============================================== **/
西兰花伟大炮
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);第一题