gpt4 book ai didi

javascript - 在 .map 中添加条件

转载 作者:行者123 更新时间:2023-11-30 13:58:04 25 4
gpt4 key购买 nike

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

代码如下:

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

但每次我都会出错。

最佳答案

在您的 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);
}

关于javascript - 在 .map 中添加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com