Javascript 如果值位于数组中,则使用包含不起作用
我有两个阵列:Javascript 如果值位于数组中,则使用包含不起作用,javascript,Javascript,我有两个阵列: ids = [a,b,c,d,e,f....]; savedRepods = [e,f]; function getPoints(){ for (var i = 0; i < savedRepods.length; i++) { if(ids.includes(savedRepods[i]) ) { console.log(savedRepods[i]); } } } 可能是因为数组条
ids = [a,b,c,d,e,f....];
savedRepods = [e,f];
function getPoints(){
for (var i = 0; i < savedRepods.length; i++) {
if(ids.includes(savedRepods[i]) ) {
console.log(savedRepods[i]);
}
}
}
可能是因为数组条目是
对象
类型<代码>数组.prototype.contains不适用于对象类型。因为在js中:
var a={
道具:“价值”
}
变量b={
道具:“价值”
}
如果(a!=b){
log('a不等于b');
}
检查重叠元素的代码工作正常。@但奇怪的是,我得到了Uncaught(in promise)TypeError:无法读取未定义的
的属性'idValue',但是当数组同时包含e
和f
时,它不包含[e,f]
@PM77-1是的,这只是一个example@PM77-1代码在ids中检查数组savePods的元素。谢谢,但我认为比较很好,我必须有一些承诺。如果我有5个ID就可以了,但是如果我加上第6个ID就不行了。我删除了单身者ID只是为了检查他们是否有问题,但没有。所以,因为我没有任何类型的限制器,比如5或6,那么在承诺的情况下,5之后就会出现问题。我只是猜测,这让我快疯了。也许这for(var I=0;I
给出了+1的承诺,这就是为什么跳过索引并产生问题的原因?@rob.m你能分享console.log(ids)
output?console.log(ids)>(51)[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,48,49,50,51]控制台日志(savedRepods)>[“33”,“35”,“26”,“20”,“18”,“24”]
/** get saved values from the server */
var savedRepods = <?php echo json_encode($userPostsInternal); ?> ;
savedRepods = savedRepods.split(",");
/** create single arrays for the values */
var date = [],
coords = [],
ids = [],
metal = [],
plastic = [],
paper = [],
glass = [],
indirizzo = [];
/** convert to a variable ALL the other values form the server */
var array = <?php echo $contents; ?> ;
/** push single values into correspondent arrays */
array.map(function(item) {
coords.push(item.Lat + "," + item.Lng);
ids.push(item.ID);
date.push(item.Date);
plastic.push(item.Plastic);
paper.push(item.Paper);
glass.push(item.Glass);
metal.push(item.Metal);
});
/**
* Now process the response from locationData
*/
var locations = getPoints();
/**
* findLatLang
*/
function findLatLang(location, geocoder, value) {
/**
* Return new Promise what resolves when
* the geocoder is successfull
* and push in the array of promises
*/
return new Promise(function(resolve, reject) {
/** Do geocoder */
geocoder.geocode({
'location': location
}, function(results, status) {
/**
* If geocoder is Ok
*/
if (status === 'OK') {
/**
* When the geocoder is successfull located
* resolve the promise and send the response of formate address
*/
resolve([results[0].formatted_address, value]);
} else {
/**
* Reject the promise
*/
reject(new Error('Couldnt\'t find the location ' + location));
}
})
})
}
/**
* processData
* return an array of promises
*/
function getPoints(){
/**
* Declare a variable of promises that have a geocoder
*/
let locationData = [];
for (var i = 0; i < savedRepods.length; i++) {
if(ids.includes(savedRepods[i]) ) {
console.log(savedRepods[i]);
var geocoder = new google.maps.Geocoder;
var latlngStr = coords[a].split(',', 2);
var latlng = {
lat: parseFloat(latlngStr[0]),
lng: parseFloat(latlngStr[1])
};
/**
* Push geocoder in array of locationdata
* Send the geocoder object on function and send the map
*/
locationData.push(findLatLang(latlng, geocoder, a))
}
}
/** return array of promises */
return locationData;
}
Promise.all(locations)
.then(function(returnVals){
indirizzo = returnVals;
doAddress(indirizzo)
});
var usedId = [],
usedMetal = [],
usedGlass = [],
usedPaper = [],
usedLocation = [],
usedPlastic = [];
const data = [];
function doAddress(indirizzo) {
indirizzo.forEach(function(item){
var a = item[1];
var location = item[0];
let newObj = {};
newObj.idValue = ids[a];
newObj.addressValue = location;
newObj.metalValue = metal[a];
newObj.glassValue = glass[a];
newObj.plasticValue = plastic[a];
newObj.paperValue = paper[a];
data.push(newObj);
$("#eachValue ul").append("<li class='list-group-item'>repod id= " + ids[a] + "<br> Indirizzo = " + location + "<br> Metallo = " + metal[a] + ", <br> Plastica = " + plastic[a] + ", <br> Vetro = " + glass[a] + ", <br> Carta = " + paper[a] + "</li>");
})
const resultMetal = data.sort((a, b) => b.metalValue - a.metalValue)[0];
const resultGlass = data.sort((a, b) => b.glassValue - a.glassValue)[0];
const resultPaper = data.sort((a, b) => b.paperValue - a.paperValue)[0];
const resultPlastic = data.sort((a, b) => b.plasticValue - a.plasticValue)[0];
$("#metal p").html("Il repod con id "+resultMetal.idValue+"<br>situato in <br>" + resultMetal.addressValue + "<br> ha consumato più metallo con un valore di " + resultMetal.metalValue);
$("#vetro p").html("Il repod con id "+resultGlass.idValue+"<br>situato in <br>" + resultGlass.addressValue + "<br> ha consumato più vetro con un valore di " + resultGlass.glassValue);
$("#plastica p").html("Il repod con id "+resultPlastic.idValue+"<br>situato in <br>" + resultPlastic.addressValue + "<br> ha consumato più plastica con un valore di " + resultPlastic.plasticValue);
$("#carta p").html("Il repod con id "+resultPaper.idValue+"<br>situato in <br>" + resultPaper.addressValue + "<br> ha consumato più carta con un valore di " + resultPaper.paperValue);
}