1

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?

I wrote this, but I want a "pretty" variant, maybe via lodash or similar:

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.. :(

2
  • 1
    What are you actually trying to do? You only need to do Math.max.apply(null,array) once, not on every loop. Commented Dec 30, 2016 at 21:41
  • It would be helpful to provide the expected output from filterScoreValues method. Commented Dec 30, 2016 at 21:42

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Use Array#map to extract the topicscore to an array. Calculate the max once using the topicscores array. Then Array#map the topicscores array into a normalized array using 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);

Comments

0

Just use javascript:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.