2016-12-30 39 views
1

也許有人知道如何通過數組的函數更改對象中的某些數據?JavaScript。如何通過函數更改數組中的某些數據?

例如,我有一個對象:

var nodes = [ 
    { topic: "Games", topicscore: 100}, 
    { topic: "Fun",  topicscore: 550}, 
    { topic: "New York", topicscore: 7799}, 
    { topic: "Hillary", topicscore: 745}, 
    { topic: "Politics", topicscore: 512}, 
    { topic: "Trump", topicscore: 71} 
]; 

而且我有一個函數:

var filterScoreValues = function(array_input){ 
    var a_out = []; 
    array_input.forEach(function(x){ 
    a_out.push(Math.ceil(x/Math.max.apply(null,array_input) * 10) + 5) 
    }); 
    return a_out; 
}; 

我怎麼能這個算法應用到我的對象的topicscore

我寫了這一點,但我希望有一個 「漂亮」 的變種,也許通過lodash或類似:在我的變種

function filterScoreValues(input){ 
    var array = input.map(function (i) { 
    return i.topicscore; 
    }); 

    array.forEach(function(x, index){ 
    input[index]['topicscore'] = Math.ceil(x/Math.max.apply(null, array) * 10) + 5; 
    }); 

    return input; 
}; 

我經常使用的環路.. :(

+1

什麼是你真正想幹什麼?你只需要執行'Math.max.apply(null,array)'一次,而不是每次循環。 – RobG

+0

提供filterScoreValues方法的預期輸出將會有所幫助。 –

回答

2

如何通過一個函數

使用以下優化方法(無功能改變中的對象的某些數據。它可以被包裹成函數如果將需要多次呼叫這種功能):

var nodes = [ 
 
    { topic: "Games", topicscore: 100}, 
 
    { topic: "Fun",  topicscore: 550}, 
 
    { topic: "New York", topicscore: 7799}, 
 
    { topic: "Hillary", topicscore: 745}, 
 
    { topic: "Politics", topicscore: 512}, 
 
    { topic: "Trump", topicscore: 71} 
 
]; 
 

 
// taking max 'topicscore' at once 
 
var max_score = Math.max.apply(null, nodes.map(function (o) { 
 
    return o.topicscore;  
 
})); 
 
// processing each 'topicscore' item in place(without creating temporary arrays) 
 
nodes.forEach(function (o) { 
 
    o.topicscore = Math.ceil(o.topicscore/max_score * 10) + 5; 
 
}); 
 

 
console.log(nodes);

0

只需用javascript:

filterScoreValues(nodes.map(node => node.topicscore)); 
1

使用Array#map提取topicscore到一個數組。計算max一次使用topicscores陣列。然後Array#maptopicscores數組轉換爲使用max的歸一化數組。

var nodes = [ 
 
    { topic: "Games", topicscore: 100}, 
 
    { topic: "Fun",  topicscore: 550}, 
 
    { topic: "New York", topicscore: 7799}, 
 
    { topic: "Hillary", topicscore: 745}, 
 
    { topic: "Politics", topicscore: 512}, 
 
    { topic: "Trump", topicscore: 71} 
 
]; 
 

 
function filterScoreValues(input){ 
 
    var topicscores = input.map(function (i) { return i.topicscore; }); // extract the topicscores to array 
 
    
 
    var max = Math.max.apply(null, topicscores); // calc the max 
 
    
 
    return topicscores.map(function (score) { // map normalize and return the new array 
 
    return Math.ceil(score/max * 10) + 5; 
 
    }); 
 
}; 
 

 
var result = filterScoreValues(nodes); 
 

 
console.log(result);

0

另一種方法是:

var nodes = [ 
    { topic: "Games", topicscore: 100}, 
    { topic: "Fun",  topicscore: 550}, 
    { topic: "New York", topicscore: 7799}, 
    { topic: "Hillary", topicscore: 745}, 
    { topic: "Politics", topicscore: 512}, 
    { topic: "Trump", topicscore: 71} 
]; 
function filterScoreValues(input){ 
    var array = input.map(function (i) { 
    return i.topicscore; 
    }); 

    array.forEach(function(x, index){ 
    input[index].topicscore = Math.ceil(x/Math.max.apply(null, array) * 10) + 5; 
    }); 

    return input; 
}; 
console.log(filterScoreValues(nodes)); 
相關問題