0

I need to fill 1 as an element of array FilteredRPeakThousandDataArray starting with index 500 in new array, and 0 for other index. Done like the following but the new array is getting filled only by 0. Explanation : at index 600,800,1200,1300 element in FilteredRPeakThousandDataArray should be 1 and 0 in all other indexes including for index 2(because 2 is less than 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);

1
  • you don't have the position 500 on FilteredRPeakDataArray, so, just compare j==i Commented Jun 20, 2019 at 9:44

8 Answers 8

2

You can simply use 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);

Sign up to request clarification or add additional context in comments.

Comments

0

you had two problems

one is that you were advancing J index anyway

and the main one was this line : FilteredRPeakDataArray[j] == i

FilteredRPeakDataArray was an array with only 5 elements and you were going with huge indices. there is better ways to implement what you trying to do but I guess you are new to js (or maybe coding) so this fix will give you a better understanding of what went wrong

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


     }

Comments

0

You can create an array with 0 for all 1500 indexes using fill. Thne loop through FilteredRPeakDataArray and update only those indexes which have value > 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]);

1 Comment

it should be n >= j instead of n > j
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);

Comments

0

I'd just pre-fill the array with 0 and then set 1 for those specific indexes:

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

Live Example:

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]}`);
}

But if you want to set those values as you build the array, you can use Array.from's callback:

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

Live Example:

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#fill and Array.from are both relatively recent additions to JavaScript, but both can be polyfilled easily.

Comments

0

You can compare j with i and then check for particular value inside FilteredRPeakDataArray array

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);

Comments

0

You could fill the 1500 element array with 0's from the start, filter the array to those element greater than j, and replace all indices with 1's:

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);

Comments

0

first fill all the elements of array with 0 's and later change the required indexes to 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); 
 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.