![](/img/trans.png)
[英]How to sum value in javascript array object form specific search id?
[英]how to sum and get max value in array object with group by id javascript
var myArray = [
{"Emiten_ID":'SMBR',"Lot":500,"Price":2500},
{"Emiten_ID":'SMBR',"Lot":300,"Price":2200},
{"Emiten_ID":'ELSA',"Lot":500,"Price":1000},
{"Emiten_ID":'SMBR',"Lot":100,"Price":3000},
{"Emiten_ID":'BI',"Lot":300,"Price":500},
{"Emiten_ID":'AAI',"Lot":200,"Price":1300},
{"Emiten_ID":'BTB',"Lot":700,"Price":2900},
{"Emiten_ID":'BI',"Lot":150,"Price":200},
{"Emiten_ID":'AAI',"Lot":200,"Price":600},
];
我想要這樣的結果,在那里我從批次和價格的最大價值得到總和
var Result= [
{"Emiten_ID":'ELSA',"Lot":500,"Price":1000},
{"Emiten_ID":'SMBR',"Lot":900,"Price":3000},
{"Emiten_ID":'BI',"Lot":450,"Price":500},
{"Emiten_ID":'BTB',"Lot":700,"Price":2900},
{"Emiten_ID":'AAI',"Lot":400,"Price":1300},
];
您可以使用Array#forEach
和一個對象作為哈希表, Emiten_ID
對其進行Emiten_ID
。
var myArray = [{ "Emiten_ID": 'SMBR', "Lot": 500, "Price": 2500 }, { "Emiten_ID": 'SMBR', "Lot": 300, "Price": 2200 }, { "Emiten_ID": 'ELSA', "Lot": 500, "Price": 1000 }, { "Emiten_ID": 'SMBR', "Lot": 100, "Price": 3000 }, { "Emiten_ID": 'BI', "Lot": 300, "Price": 500 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 1300 }, { "Emiten_ID": 'BTB', "Lot": 700, "Price": 2900 }, { "Emiten_ID": 'BI', "Lot": 150, "Price": 200 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 600 }, ], result = []; myArray.forEach(function (a) { if (!this[a.Emiten_ID]) { this[a.Emiten_ID] = { Emiten_ID: a.Emiten_ID, Lot: 0, Price: 0 }; result.push(this[a.Emiten_ID]); } this[a.Emiten_ID].Lot += a.Lot; this[a.Emiten_ID].Price = Math.max(this[a.Emiten_ID].Price, a.Price); }, Object.create(null)); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6帶有hash
的閉包而不使用this
。
var myArray = [{ "Emiten_ID": 'SMBR', "Lot": 500, "Price": 2500 }, { "Emiten_ID": 'SMBR', "Lot": 300, "Price": 2200 }, { "Emiten_ID": 'ELSA', "Lot": 500, "Price": 1000 }, { "Emiten_ID": 'SMBR', "Lot": 100, "Price": 3000 }, { "Emiten_ID": 'BI', "Lot": 300, "Price": 500 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 1300 }, { "Emiten_ID": 'BTB', "Lot": 700, "Price": 2900 }, { "Emiten_ID": 'BI', "Lot": 150, "Price": 200 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 600 }, ], result = []; myArray.forEach((hash => a => { if (!hash[a.Emiten_ID]) { hash[a.Emiten_ID] = { Emiten_ID: a.Emiten_ID, Lot: 0, Price: 0 }; result.push(hash[a.Emiten_ID]); } hash[a.Emiten_ID].Lot += a.Lot; hash[a.Emiten_ID].Price = Math.max(hash[a.Emiten_ID].Price, a.Price); })(Object.create(null))); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用linq.js庫:
var myArray = [ {"Emiten_ID":'SMBR',"Lot":500,"Price":2500}, {"Emiten_ID":'SMBR',"Lot":300,"Price":2200}, {"Emiten_ID":'ELSA',"Lot":500,"Price":1000}, {"Emiten_ID":'SMBR',"Lot":100,"Price":3000}, {"Emiten_ID":'BI',"Lot":300,"Price":500}, {"Emiten_ID":'AAI',"Lot":200,"Price":1300}, {"Emiten_ID":'BTB',"Lot":700,"Price":2900}, {"Emiten_ID":'BI',"Lot":150,"Price":200}, {"Emiten_ID":'AAI',"Lot":200,"Price":600}, ]; var answer = Enumerable.From(myArray).GroupBy("x => x.Emiten_ID", "x => {Lot: x.Lot, Price: x.Price}").Select("x => {Emiten_ID:x.Key(), Lot:x.Sum(y=>y.Lot), Price:x.Max(y=>y.Price)}").ToArray(); answer.forEach(x => console.log(x));
<script data-require="jquery@3.0.0" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> <script data-require="linq.js@2.2.0+2" data-semver="2.2.0+2" src="//cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script> <script data-require="linq.js@2.2.0+2" data-semver="2.2.0+2" src="//cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/jquery.linq.js"></script>
使用ECMAScript 2015(或polyfill)中提供的Map類:
var temp = new Map();
for (var item of myArray) {
var e = temp.get(item.Emiten_ID)
if (e) {
e.Lot += item.Lot;
e.Price = Math.max( e.Price, item.Price );
} else {
temp.set( item.Emiten_ID,
{ Emiten_ID: item.Emiten_ID, Lot:item.Lot, Price:item.Price })
}
}
var result = Array.from(temp.values());
console.log(result)
這是很長的路要走,希望有更簡單的方法做到這一點。
var myArray = [
{"Emiten_ID":'SMBR',"Lot":500,"Price":2500},
{"Emiten_ID":'SMBR',"Lot":300,"Price":2200},
{"Emiten_ID":'ELSA',"Lot":500,"Price":1000},
{"Emiten_ID":'SMBR',"Lot":100,"Price":3000},
{"Emiten_ID":'BI',"Lot":300,"Price":500},
{"Emiten_ID":'AAI',"Lot":200,"Price":1300},
{"Emiten_ID":'BTB',"Lot":700,"Price":2900},
{"Emiten_ID":'BI',"Lot":150,"Price":200},
{"Emiten_ID":'AAI',"Lot":200,"Price":600},
];
var sortedIds = [];
var Result = [];
function sortArray(){
for(var i = 0; i < myArray.length; i++){
if(myArray.indexOf(myArray[i].Emiten_ID) < 0){
sortedIds.push(myArray[i].Emiten_ID);
Result.push({
"Emiten_ID" : myArray[i].Emiten_ID,
"Lot" : sumLot(myArray[i].Emiten_ID),
"Price" : maxPrice(myArray[i].Emiten_ID);
});
}
}
//out put
console.log(Result);
}
function sumLot(id){
var sum = 0;
for(var i = 0; i < myArray.length; i++){
if(myArray[i].Emiten_ID == id){
sum += myArray[i].lot;
}
}
return sum;
}
function maxPrice(id){
var max = 0;
for(var i = 0; i < myArray.length; i++){
if(myArray[i].Price > max){
max = myArray[i].Price;
}
}
return max;
}
var myArray = [ {"Emiten_ID":'SMBR',"Lot":500,"Price":2500}, {"Emiten_ID":'SMBR',"Lot":300,"Price":2200}, {"Emiten_ID":'ELSA',"Lot":500,"Price":1000}, {"Emiten_ID":'SMBR',"Lot":100,"Price":3000}, {"Emiten_ID":'BI',"Lot":300,"Price":500}, {"Emiten_ID":'AAI',"Lot":200,"Price":1300}, {"Emiten_ID":'BTB',"Lot":700,"Price":2900}, {"Emiten_ID":'BI',"Lot":150,"Price":200}, {"Emiten_ID":'AAI',"Lot":200,"Price":600}, ]; myArray = myArray.sort(function(a,b){ if(a.Emiten_ID > b.Emiten_ID) return 1; else if(a.Emiten_ID < b.Emiten_ID) return -1; else{ return a.Price - b.Price; } }); var result = [myArray[0]]; for(var i = 1 ; i < myArray.length ; i ++){ var obj = myArray[i]; var res = result[result.length - 1]; if(obj.Emiten_ID == res.Emiten_ID){ res.Lot += obj.Lot; res.Price = Math.max(res.Price,obj.Price); }else{ result.push(obj); } } console.log(result);
使用lodash的解決方案:
_.chain(myArray).groupBy('Emiten_ID').map(emiten => ({
"Emiten_ID":emiten[0]['Emiten_ID'],
"Lot": _.sumBy(emiten, 'Lot'),
"Price": _.maxBy(emiten, 'Price')['Price']
})).value()
array
元素 each-element
的索引 array
Lot
的值相加,如果Price
值更高,則覆蓋更高的值 var myArray = [{ "Emiten_ID": 'SMBR', "Lot": 500, "Price": 2500 }, { "Emiten_ID": 'SMBR', "Lot": 300, "Price": 2200 }, { "Emiten_ID": 'ELSA', "Lot": 500, "Price": 1000 }, { "Emiten_ID": 'SMBR', "Lot": 100, "Price": 3000 }, { "Emiten_ID": 'BI', "Lot": 300, "Price": 500 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 1300 }, { "Emiten_ID": 'BTB', "Lot": 700, "Price": 2900 }, { "Emiten_ID": 'BI', "Lot": 150, "Price": 200 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 600 }]; var newArr = []; myArray.forEach(function (el) { var findIndex = newArr.findIndex(function (item) { return item.Emiten_ID === el.Emiten_ID; }); if (findIndex === -1) { newArr.push(el); } else if (el.Price > newArr[findIndex].Price) { newArr[findIndex].Price = el.Price; newArr[findIndex].Lot += el.Lot; } else { newArr[findIndex].Lot += el.Lot; } }); console.log(JSON.stringify(newArr, null, 4));
讓我們嘗試分解這個。 我們將通過編寫一個通用的效用函數,我們將調用start combine
,其結合使用在哈希叫每個屬性指定的函數對象的屬性combiners
:
function combine(array, combiners) {
const result = {};
for (prop of Object.keys(combiners)) {
result[prop] = combiners[prop](...array.map(elt => elt[prop]));
}
return result;
}
使用此示例:
combine(
[{a: 1, b: 10}, {a: 42, b: 80}],
{a: sum, b: Math.max}
)
這將導致
{a: 43, b: 80}
當然,為了完成這項工作,我們必須定義sum
:
function sum(...vals) { return vals.reduce(add); }
其中add
就是
function add(a, b) { return a + b; }
接下來,我們將按Emiten_ID
屬性對輸入進行Emiten_ID
。 您可以使用_.groupBy
的_.groupBy
,或者自己編寫(見下文)。
const groups = _.groupBy(myArray, 'Emiten_ID`);
這將導致看起來像
{ SMBR: [
{ Emiten_ID: 'SMBR', "Lot": 500, "Price": 2500},
{ Emiten_ID: 'SMBR', "Lot": 300, "Price": 2200}
],
ELSA: [
]
}
完成此准備工作后,只需使用我們的combine
實用程序映射groups
每個值,就可以輕松獲得結果:
const Result = Object.keys(groups).map(key =>
combine(
groups[key],
{
Emiten_ID: identity,
Lot: sum,
Price: Math.max}));
identity
是公正的
function identity(id) { return id; }
如果您希望在映射對象屬性的combine
中抽象出概念,您可以再次使用Underscore中的一些實用程序,或者自己編寫它:
function mapObject(obj, fn) {
const result = {};
for (prop of obj) result[prop] = fn(obj[prop], prop);
return result;
}
使用此示例:
mapObject({a: 2, b: 3}, x => x * x)
// yields {a: 4, b: 9}
現在你可以更簡單地編寫combine
了
function combine(array, combiners) {
return mapObject(combiners, function(combiner, prop) {
return combiner(...array.map(elt => elt[prop]));
};
}
如果你不想使用_.groupBy
的_.groupBy
,這里是一個本土版本:
function groupBy(array, prop) {
var result = {};
array.forEach(elt => (result[elt[prop]] = result[elt[prop]] || []).push(elt));
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.