首页 > 解决方案 > 在 .map 中添加条件

问题描述

这是我想从列表中随机选择一个城市的代码,根据它的权重,然后根据城市之间的距离修改权重(选择城市的距离越大,权重越大)。在此之后,我再次使用修改后的列表随机选择另一个城市。代码工作正常,除了一件事......

这是代码:

// list of cities
var liste = [
   { name: "New York", distance: 12, weight: 5},
   { name: "Atlanta", distance: 4, weight: 4},
   { name: "Dallas", distance: 2, weight: 2},
   { name: "Los Angeles", distance: 1, weight: 1},
];;

var repeatTimes = 4;
var choose = [];
choose = [liste.map(x=>x.name), liste.map(x=>x.distance), liste.map(x=>x.weight)];

// randomaly pick a city based on its weight
var rand = function(min, max) {
    return Math.random() * (max - min) + min;
};

var getRandomItem = function(choose, weight) {

    var total_weight = weight.reduce(function (prev, cur, i, arr) {
        return prev + cur;
    });

    var random_num = rand(0, total_weight);
    var weight_sum = 0;
    //console.log(random_num)

    for (var i = 0; i < choose.length; i++) {
        weight_sum += weight[i];
        weight_sum = +weight_sum.toFixed(2);

        if (random_num <= weight_sum) {
            return choose[i];
        }
    }
    // end of function, modify the weights of list again
        
};

  // for the first time pick a city randomaly
  var random_item = getRandomItem(choose[0], choose[2]);
  console.log(random_item);

 newWeights();

// after the first time of picking cities let's modify the weights of list
function newWeights(){
var baseDistance = liste.find(l => l.name == random_item).distance;
  
console.log(choose[0].indexOf(random_item));
 
var list = liste.map(c => {
  var newWeight = Math.abs(baseDistance - c.distance);
  return {
    name: c.name,
    distance: c.distance,
    weight: newWeight
  };
 
});  
liste = list.filter(function(value){

    return value.weight != 0;

});  
choose = [liste.map(x=>x.name), liste.map(x=>x.distance), liste.map(x=>x.weight)];
console.log(liste);
}

// use the modified list to randomaly picking another city
 for (var i = 1; i < repeatTimes; i++) {     
   
       var random_item = getRandomItem(choose[0], choose[2]);
       console.log(random_item);   
        newWeights();
 }
//

这是我创建包含修改后的列表的列表数组的地方。

var list = liste.map(c => {
  var newWeight = Math.abs(baseDistance - c.distance);
  return {
    name: c.name,
    distance: c.distance,
    weight: newWeight
  };

});

我想要的只是将每个对象的权重添加到newWeight中,如下所示:

if(newWeight !== 0){
var newWeight = Math.abs(baseDistance - c.distance) + c.weight;
}

但每次我得到错误。

标签: javascript

解决方案


在您的 newWeights 方法中,您重新分配了一个新值以列出...

看看这个:https ://jsfiddle.net/gbc9w06y/

// list of cities
var liste = [
   { name: "New York", distance: 8, weight: 1 },
   { name: "Atlanta", distance: 4, weight: 1 },
   { name: "Dallas", distance: 2, weight: 1 },
   { name: "Los Angeles", distance: 1, weight: 1 },
];

var repeatTimes = 4;
var choose = [];
choose = [liste.map(x=>x.name), liste.map(x=>x.distance), liste.map(x=>x.weight)];

// randomaly pick a city based on its weight
var rand = function(min, max) {
    return Math.random() * (max - min) + min;
};

var getRandomItem = function(choose, weight) {

    var total_weight = weight.reduce(function (prev, cur, i, arr) {
        return prev + cur;
    });

    var random_num = rand(0, total_weight);
    var weight_sum = 0;
    //console.log(random_num)

    for (var i = 0; i < choose.length; i++) {
        weight_sum += weight[i];
        weight_sum = +weight_sum.toFixed(2);

        if (random_num <= weight_sum) {
            return choose[i];
        }
    }
    // end of function, modify the weights of list again
       newWeights();
};

  // for the first time pick a city randomaly
  var random_item = getRandomItem(choose[0], choose[2]);
  console.log(random_item);

newWeights();

// after the first time of picking cities let's modify the weights of list
function newWeights(){
var baseDistance = liste.find(l => l.name == random_item).distance;

var list = liste.map(c => {
  console.log();
  var newWeight = Math.abs(baseDistance - c.distance);
  return {
    name: c.name,
    distance: c.distance,
    weight: newWeight
  };
});
liste = list;
 choose = [liste.map(x=>x.name), liste.map(x=>x.distance), liste.map(x=>x.weight)];
console.log(list);
}

// use the modified list to randomaly picking another city
 for (var i = 1; i < repeatTimes; i++) {        
       var random_item = getRandomItem(choose[0], choose[2]);
       console.log(random_item);          
 }

推荐阅读