用Java数组填充

时间:2019-06-20 09:38:01

标签: javascript arrays

我需要将1作为数组FilteredRPeakThousandDataArray的元素填充,从新数组的索引500开始,其他索引为0。完成以下操作,但是新数组仅被0填充。说明:在索引FilteredRPeakThousandDataArray的索引600,800,1200,1300中,元素应为1,在包括索引2在内的所有其他索引中为0(因为2小于500)

var j = 500;
var FilteredRPeakDataArray = new Array();
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = new Array();

for (var i = 0; i < 1500; i++) {
  if (FilteredRPeakDataArray[j] == i) {
    FilteredRPeakThousandDataArray[i] = 1;
  } else {
    FilteredRPeakThousandDataArray[i] = 0;
  }
  
  j++;
}

console.log("FilteredRPeakThousandDataArray " + FilteredRPeakThousandDataArray);

8 个答案:

答案 0 :(得分:2)

您可以简单地使用Array.from()。

const FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];

const FilteredRPeakThousandDataArray = Array.from({length: 1500},
 (_, i) => FilteredRPeakDataArray.includes(i) && i > 500 ? 1 : 0
);
console.log(FilteredRPeakThousandDataArray);

答案 1 :(得分:0)

您遇到两个问题

一个是您仍在推进J索引

主要是这一行:FilteredRPeakDataArray[j] == i

FilteredRPeakDataArray是一个只有5个元素的数组,您要使用巨大的索引。 有更好的方法来实现您要尝试执行的操作,但是我想您是js(或也许是编码)的新手,因此此修补程序将使您更好地了解出了什么问题

var startFrom = 500;
var j=0;
var FilteredRPeakDataArray = new Array();
var FilteredRPeakDataArray = [2,600,800,1200,1300];
var FilteredRPeakThousandDataArray = new Array();

        for (var i=0;i<1500;i++){
      if(FilteredRPeakDataArray[j]==i){
if( i > startFrom ){
        FilteredRPeakThousandDataArray[i] = 1;
}
        j++;
      }
      else{
        FilteredRPeakThousandDataArray[i] = 0;
      }


     }

答案 2 :(得分:0)

首先用0填充数组的所有元素,然后将所需的索引更改为1

var j = 500;
var FilteredRPeakDataArray = new Array();
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = new Array(1500).fill(0);

for (var i = 0; i < FilteredRPeakDataArray.length; i++) {
    
  FilteredRPeakDataArray[i]>=j ? FilteredRPeakThousandDataArray[FilteredRPeakDataArray[i]]=1:null
  

}

console.log("FilteredRPeakThousandDataArray " + FilteredRPeakThousandDataArray); 
 

答案 3 :(得分:0)

您可以使用fill为所有1500个索引使用0创建一个数组。遍历FilteredRPeakDataArray并仅更新值> j

的那些索引

var j = 500;
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = Array(1500).fill(0);

FilteredRPeakDataArray.forEach(n => {
  if(n > j)
    FilteredRPeakThousandDataArray[n] = 1;
})

console.log(FilteredRPeakThousandDataArray[2]);
console.log(FilteredRPeakThousandDataArray[600]);
console.log(FilteredRPeakThousandDataArray[800]);

答案 4 :(得分:0)

const start = 500;
const limit = 1500;
const FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
// fill array with 1s
const FilteredRPeakThousandDataArray = new Array(limit).fill(1);
// change value of indices stored in FilteredRPeakDataArray to 0
for (const idx of FilteredRPeakDataArray) {
   if (idx >= start) {
      FilteredRPeakThousandDataArray[idx] = 0;
   }
}
console.log("FilteredRPeakThousandDataArray " + FilteredRPeakThousandDataArray);

答案 5 :(得分:0)

我只是用0预填充数组,然后为这些特定索引设置1:

var FilteredRPeakThousandDataArray = new Array(1500);
FilteredRPeakThousandDataArray.fill(0);
for (const index of FilteredRPeakDataArray) {
    if (index >= j) {
        FilteredRPeakThousandDataArray[index] = 1;
    }
}

实时示例:

var j = 500;
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = new Array(1500);
FilteredRPeakThousandDataArray.fill(0);
for (const index of FilteredRPeakDataArray) {
    if (index >= j) {
        FilteredRPeakThousandDataArray[index] = 1;
    }
}

// Show results
for (const index of [0, ...FilteredRPeakDataArray]) {
    console.log(`FilteredRPeakThousandDataArray[${index}] = ${FilteredRPeakThousandDataArray[index]}`);
}

但是,如果要在构建数组时设置这些值,则可以使用Array.from的回调:

var FilteredRPeakThousandDataArray = Array.from({length: 1500}, (_, index) => {
    return index >= j && FilteredRPeakDataArray.includes(index) ? 1 : 0;
});

实时示例:

var j = 500;
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = Array.from({length: 1500}, (_, index) => {
    return index >= j && FilteredRPeakDataArray.includes(index) ? 1 : 0;
});

// Show results
for (const index of [0, ...FilteredRPeakDataArray]) {
    console.log(`FilteredRPeakThousandDataArray[${index}] = ${FilteredRPeakThousandDataArray[index]}`);
}


Array#fillArray.from都是JavaScript的较新版本,但是可以很容易地进行填充。

答案 6 :(得分:0)

您可以将ji进行比较,然后检查FilteredRPeakDataArray数组中的特定值

var j = 500;
var FilteredRPeakDataArray = new Array();
var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];
var FilteredRPeakThousandDataArray = new Array(1500).fill(0);

for (var i = 0; i < 1500; i++) {
  if (i >= j && FilteredRPeakDataArray.includes(i)) {
    FilteredRPeakThousandDataArray[i] = 1;
  } else {
    FilteredRPeakThousandDataArray[i] = 0;
  }
}

console.log( FilteredRPeakThousandDataArray);

答案 7 :(得分:0)

您可以从头开始用0填充1500个元素数组,将其过滤到大于j的元素,然后用1替换所有索引:

var j = 500;

var FilteredRPeakDataArray = [2, 600, 800, 1200, 1300];

var FilteredRPeakThousandDataArray = new Array(1500).fill(0);

FilteredRPeakDataArray.filter(el => el > j).map(el => FilteredRPeakThousandDataArray[el] = 1)

console.log("FilteredRPeakThousandDataArray " + FilteredRPeakThousandDataArray);