JavaScript的。如何通过数组函数更改对象中的某些数据?

时间:2016-12-30 21:32:53

标签: javascript arrays object lodash

也许有人知道如何通过数组函数更改对象中的某些数据?

例如,我有一个对象:

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

我经常在我的变种上使用循环.. :(

4 个答案:

答案 0 :(得分:2)

  

如何通过函数

更改对象中的某些数据

使用以下优化方法(没有函数。如果需要多次调用此类函数,可以将其包含在函数中):



dat <- structure(list(Value = c(32, 5, 18, 3, 16, 14, 28, 28, 49, 15, 
43, 49, 40, 17, 9, 31, 8, 43, 50, 48, 11, 42, 0, 15, 8, 1, 41, 
15, 4, 31), Mutation = c("Yes", "no", "no", "no", "no", "Yes", 
"Yes", "Yes", "Yes", "Yes", "no", "Yes", "Yes", "Yes", "no", 
"Yes", "Yes", "no", "Yes", "Yes", "Yes", "no", "Yes", "Yes", 
"no", "Yes", "no", "no", "no", "Yes"), Group = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("Value", 
"Mutation", "Group"), class = "data.frame", row.names = c(NA, 
-30L))
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用Array#maptopicscore提取到数组中。使用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);转换为规范化数组。

&#13;
&#13;
$("frames".eq(i))...
$("frames".eq(x))...
&#13;
&#13;
&#13;

答案 2 :(得分:0)

只需使用javascript:

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

答案 3 :(得分: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));