Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

javascript - Iterate in objects arrays and check for identical values

I have this code, take the divisors of a number and filter to be a prime divisor.

function check_prime(number) {
  var the_divs = [];
  for(var i=0; i<=number; i++){ if(number % i === 0) { the_divs.push(i); } }
  return the_divs.length == 2;
}
function sumOfDivided(lst) {
  var obj = {}
  for(var i in lst){
    var the_number = lst[i];
    if(the_number) {
      var the_divs = [];
      for(var j=0; j<=the_number; j++){
        if(the_number % j === 0){ the_divs.push(j); }
      }
      obj[the_number] = the_divs;
    }  
  }
  for(var k in obj){ obj[k] = obj[k].filter((p) => check_prime(p)); }
  console.log(obj)
  }
   console.log(sumOfDivided([12, 15]));

The obj will return : { 12:[2,3], 15:[3, 5] }

What I'm trying to do is to get the same values from the obj, that is 3! I need to return the numbers with the same divisor too: 3 from 12, 15;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...