2016-09-20 38 views
5
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}, 
]; 

我想造成這樣的,我在那裏得到的價格如何總結和ID的JavaScript

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}, 
]; 
+2

我沒有看到任何JavaScript(除了問題和期望的結果之外),你用JavaScript試過了什麼?請提供[mcve]。 – zer00ne

回答

2

從很多,最大值和您可以使用Array#forEach並獲得與組數組對象最大值一個對象作爲散列表並按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; }

+0

這個答案中提到了什麼? –

+1

它是指thisArg:'的Object.create(空)'。 –

+0

Hi @NinaScholz。我知道你愛用'thisArg'參數'forEach'等作爲一種免費的上下文變量,但是基於上面的評論我想這不僅是我是誰發現它有點反直覺。 – 2016-09-20 06:55:43

1

這是很長的路要走這一點, 希望有更簡單的方法做到這一點。

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; 
} 
1

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){ 
 
\t if(a.Emiten_ID > b.Emiten_ID) 
 
\t \t return 1; 
 
\t else if(a.Emiten_ID < b.Emiten_ID) 
 
\t \t return -1; 
 
\t else{ 
 
\t \t return a.Price - b.Price; 
 
\t } 
 
}); 
 

 
var result = [myArray[0]]; 
 

 
for(var i = 1 ; i < myArray.length ; i ++){ 
 
\t var obj = myArray[i]; 
 
\t var res = result[result.length - 1]; 
 
\t 
 
\t if(obj.Emiten_ID == res.Emiten_ID){ 
 
\t \t res.Lot += obj.Lot; 
 
\t \t res.Price = Math.max(res.Price,obj.Price); 
 
\t }else{ 
 
\t \t result.push(obj); 
 
\t } 
 

 
} 
 

 
console.log(result);

2

您可以使用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="[email protected]" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> 
 
<script data-require="[email protected]+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="[email protected]+2" data-semver="2.2.0+2" src="//cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/jquery.linq.js"></script>

1

解決方案使用lodash:

_.chain(myArray).groupBy('Emiten_ID').map(emiten => ({ 
    "Emiten_ID":emiten[0]['Emiten_ID'], 
    "Lot": _.sumBy(emiten, 'Lot'), 
    "Price": _.maxBy(emiten, 'Price')['Price'] 
})).value() 
1
  • 迭代所有array元件
  • 查找each-element從新的數組的索引
  • 如果元件不存在,將其推入array
  • 如果元素存在,總結的Lot的值,且如果值Price是heigher,過乘坐heigher值

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));

1

讓我們試着分解這個。

combine(
    [{a: 1, b: 10}, {a: 42, b: 80}], 
    {a: sum, b: Math.max} 
) 

function combine(array, combiners) { 
    const result = {}; 
    for (prop of Object.keys(combiners)) { 
    result[prop] = combiners[prop](...array.map(elt => elt[prop])); 
    } 
    return result; 
} 

使用這個示例:我們將通過編寫一個通用的效用函數,我們將調用combine,它結合了使用在哈希每個屬性指定的函數調用combiners對象的屬性開始

這將導致

{a: 43, b: 80} 

當然,要完成這項工作,我們必須定義sum

function sum(...vals) { return vals.reduce(add); } 

其中add只是

function add(a, b) { return a + b; } 

接下來,我們將通過集團的Emiten_ID屬性的輸入。您可以使用Underscore的_.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,你可以再次使用從下劃線一些實用工具,或自己編寫:使用本的

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,這裏是一個土生土長的版本:

function groupBy(array, prop) { 
    var result = {}; 
    array.forEach(elt => (result[elt[prop]] = result[elt[prop]] || []).push(elt)); 
    return result; 
} 
+0

'const Result = Object。鍵(組).map(key => return combine('你可以跳過'return') –

2

使用的ECMAScript 2015(或填充工具)提供地圖類:

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) 
+0

我認爲這是因爲你將值作爲屬性添加到屬性而不是條目中,你應該做'map.set ',對嗎? – 2016-09-20 07:05:40

+0

你的權利@torazaburo,這是有效的。我已經更新了我的答案。 –

+0

for循環的第一行應該是'temp.get',對吧? – 2016-09-20 16:42:52