如何返回数组中每个对象的数组位置。
let targets = [1.2, 2.3, 3.5];
let targetUpdatedOn = [2018-07-06, 2018-07-06, 2018-07-06];
let liveCoinPrice = 1.3;
let targets_hit = targets.filter(function(target_value) {
return liveCoinPrice >= target_value;
});
This what is done to find array position.
targets_hit.forEach(function(key) {
if(targetUpdatedOn[key] === undefined){
console.log(targetUpdatedOn);
}
}
我想返回每个targets_hits的数组位置。任何帮助将是感激的。
答案 0 :(得分:3)
您可以执行此操作。
targets_hit.forEach((item, index) => {
console.log(item, index); // Item and index
console.log(index) // Index only
});
forEach
方法回调具有3个参数。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
答案 1 :(得分:3)
这是您使用JavaScript中的array#forEach
查找每个数组对象的位置的方法:
let targets = [1.2, 2.3, 3.5];
targets.forEach((element, index) => {
console.log(`Object is ${element} and it's position is ${index}`);
});