I have an array that I created using Array(...)
and Array.prototype.map
, like this:
var array_name = Array(255).map(function(undef, i) {
return i + 1;
});
The values of this array are:
[1, 2, 3, ..., 253, 254, 255]
This is an array that won't get modified, so the first value of this array will always be 1
and the last value of this array will always be 255
.
I already know the index of each value is {value} - 1
, so 200
would be 199
, 199
would be 198
, so on and so forth.
Let's say I want 255
's opposite value, which would be 0
, I could get that using array_name[0]
, but what if I wanted 200
's opposite value, how would I know what the opposite index of 199
is so I could get it's value?
I have an array that I created using Array(...)
and Array.prototype.map
, like this:
var array_name = Array(255).map(function(undef, i) {
return i + 1;
});
The values of this array are:
[1, 2, 3, ..., 253, 254, 255]
This is an array that won't get modified, so the first value of this array will always be 1
and the last value of this array will always be 255
.
I already know the index of each value is {value} - 1
, so 200
would be 199
, 199
would be 198
, so on and so forth.
Let's say I want 255
's opposite value, which would be 0
, I could get that using array_name[0]
, but what if I wanted 200
's opposite value, how would I know what the opposite index of 199
is so I could get it's value?
- 1 do you have an example what you like to do (in code form)? – Nina Scholz Commented Mar 16, 2019 at 14:50
-
3
You can use
reverse()
for opposite index – Maheer Ali Commented Mar 16, 2019 at 14:52 -
Whoops. Yes, the opposite value for
255
is0
, it was an honest mistake and I fixed it. – Malekai Commented Mar 16, 2019 at 14:53 - 2 array_name[(array_name.length-1) - index] – Serene Abraham Mathew Commented Mar 16, 2019 at 14:55
- @NinaScholz Are you asking if I can create sample code to produce the desired result? If so, that won't be possible because I don't know how to do that. – Malekai Commented Mar 16, 2019 at 14:56
6 Answers
Reset to default 4Do:
opposite_index = arr.length - index - 1
For example:
a = [1,2,3,4,5,6,7,8,9,10]
index = 3
a[index]
4
It's opposite is 7 so:
opposite_index = a.length - index - 1
a[opposite_index]
7
With reverse
as per @Maheer Ali suggestion:
a.reverse()[index]
7
Your Array(255).map()
create undefined array value.So do with Array#from
length object.And pass your value.get index of the value and match with reverse array you get opposite value
let check = (val) => {
var array_name = Array.from({length:255},(a,b)=>b+1);
var nor_ind = array_name.indexOf(val);
var re_in = array_name.reverse(array_name).indexOf(val)
return ({nor_val:val,nor_ind:nor_ind,opp_val:re_in})
}
console.log(check(254))
First of all the code you provided doesn't create array [1,2,3...255]
. It will create it will 255 empty items first you need to fill()
.
var arr = Array(255).fill().map((a,i) => i+1);
//Create an array which will have revese items.
let revarr= arr.reverse()
console.log(revarr[0]) //255
console.log(revarr[254]) // 1
If you don't want to create a reverse arr. You can create a function
var arr = Array(255).fill().map((a,i) => i+1);
const opp = (arr,num) => arr[arr.length - num - 1];
console.log(opp(arr,0));
console.log(opp(arr,254));
First, you gotta understand that there is weird behavior concerning Array(n).map(f)
(it won't create the array you're expecting), see this answer for explanation, second, do this to get the opposite values:
/* fill it first with .fill(), see the question I linked for more explanation */
var array = Array(255).fill(undefined).map(function(undef, i) {
return i + 1;
});
function opposite(array, n) {
return array[array.length - n];
}
console.log(opposite(array, 255));
console.log(opposite(array, 200));
console.log(opposite(array, 199));
console.log(opposite(array, 1));
Notice that length - n
is used instead of length - n - 1
, because we're dealing with values from 1
to n
, not from 0
to n - 1
.
Subtract the index from (length-1) -> max index of the array
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function findOpp(index, length) {
maxIndex = length - 1;
if (index <= maxIndex && index >= 0) {
return maxIndex - index;
} else {
return 'You have enter a wrong index';
}
}
console.log(findOpp(-1, 10));
console.log(findOpp(0, 10));
console.log(findOpp(1, 10));
console.log(findOpp(2, 10));
console.log(findOpp(4, 10));
console.log(findOpp(5, 10));
console.log(findOpp(6, 10));
console.log(findOpp(7, 10));
console.log(findOpp(8, 10));
console.log(findOpp(9, 10));
console.log(findOpp(10, 10));
Using Maheer Ali's suggestion I managed to get the desired result by reversing the array and using indexOf
to get the index of that number:
var numbers = Array(255).map(function(v, i) {
return i + 1;
});
var opposite_brightness = numbers.reverse()[numbers.indexOf(brightness)];