[英]JavaScript. How to change some data in object via function for array?
Maybe somebody knows how to change some data in an object via a function for an array? 也许有人知道如何通过数组函数更改对象中的某些数据?
For example, I have an object: 例如,我有一个对象:
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}
];
And I have a function: 我有一个功能:
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;
};
How I can apply this algorithm to topicscore
of my object? 如何将这种算法应用于对象的topicscore
?
I wrote this, but I want a "pretty" variant, maybe via lodash or similar: 我写了这个,但是我想要一个“漂亮的”变体,也许通过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;
};
I often used loops on my variant.. :( 我经常在变量上使用循环。
how to change some data in an object via a function 如何通过函数更改对象中的某些数据
Use the following optimized approach( without a function. It may be wrapped into function if there would be a need to call such function multiple times ): 使用以下优化方法( 不带函数。如果需要多次调用该函数,可以将其包装到函数中 ):
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);
Use Array#map
to extract the topicscore
to an array. 使用Array#map
将topicscore
提取到数组。 Calculate the max
once using the topicscores
array. 使用topicscores
数组一次计算max
。 Then Array#map
the topicscores
array into a normalized array using max
. 然后Array#map
使用max
将topicscores
数组Array#map
到规范化数组中。
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);
只需使用javascript:
filterScoreValues(nodes.map(node => node.topicscore));
Another alternative is: 另一种选择是:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.