As logically concluded by the linq example: None is the same as !Any, so you could define your own utility method like this:
const none = (arr, callback) => !arr.some(callback)
And then call like this:
const arr = ["a","b","c"]
const hasNoCs = none(arr, el => el === "c") // false
const hasNoDs = none(arr, el => el === "d") // true
Demo in Stack Snippets
const none = (arr, callback) => !arr.some(callback)
const arr = ["a","b","c"]
const hasNoCs = none(arr, el => el === "c") // false
const hasNoDs = none(arr, el => el === "d") // true
console.log({hasNoCs, hasNoDs})
Answer from KyleMit on Stack OverflowAs logically concluded by the linq example: None is the same as !Any, so you could define your own utility method like this:
const none = (arr, callback) => !arr.some(callback)
And then call like this:
const arr = ["a","b","c"]
const hasNoCs = none(arr, el => el === "c") // false
const hasNoDs = none(arr, el => el === "d") // true
Demo in Stack Snippets
const none = (arr, callback) => !arr.some(callback)
const arr = ["a","b","c"]
const hasNoCs = none(arr, el => el === "c") // false
const hasNoDs = none(arr, el => el === "d") // true
console.log({hasNoCs, hasNoDs})
Not at the moment
At the moment I'm using an Array.some() and then negating the whole thing
Personally I think an Array.none function would be awesome
You could request it to be added to the next release of ECMA. It looks like a bit of a process to say the least but if we wanted to get this added to ECMA this would be the way.
Videos
Do:
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))
// These days you can do it in one line:
const unfold = (accumulator, length) => length <= 0 ? accumulator : unfold([length, ...accumulator], length -1)
// invoke it like this:
const results = unfold([], 5)
// expected results: 1,2,3,4,5
console.log(results.join(','))
Since we're looking for a concise way to generate a given number of elements as an array, this "unfold" function does it with recursion.
The first argument is the accumulator array. This needs to be passed along, and eventually is returned when it holds the entire collection. The second argument is the limiter. This is what you use to dimension your resulting array.
In each call, we first test if the base case is reached. If so, the answer is easy: just return the given array. For the general case, we are again unfolding, but with a smaller value, so we prepend one value to accumulator, and decrement length.
Since we're using the spread operator and a 'computed-if' the function is concise. Using the arrow style also lets us avoid the 'function' and 'return' keywords, as well as curly-braces. So the whole thing is a one-liner.
I basically use this technique as a for-loop substitute for React JSX, where everything needs to be an expression (Array.map()).
The opposite of Array#reduce in Javascript is Array.from (or alternatively spread syntax). You can use it with any iterable object to generate an array:
array = Array.from(iterator); // same as array = [...iterator];
You can create an iterator by calling a generator function:
iterator = generate(params);
Generator functions use the special keyword yield to return their results (or yield* to return all results from another iterable). And they are depleted once they return:
function* convertBase(wet, alphabet) {
const base = BigInt(alphabet.length);
wet = BigInt(wet);
while (wet > 0) {
const digitVal = Number(wet % base);
wet = wet / base;
yield alphabet.charAt(digitVal);
}
}
console.log(Array.from(convertBase(100308923948716816384684613592839, "0123456789ABCDEFGH")).reverse().join(""));
Alternatively you can implement the iterator yourself without a generator function:
console.log(Array.from({
wet: BigInt(100308923948716816384684613592839),
base: BigInt(18),
alphabet: "0123456789ABCDEFGH",
[Symbol.iterator]: function() {
return this;
},
next: function() {
if (this.wet > 0) {
const digitVal = Number(this.wet % this.base);
this.wet = this.wet / this.base;
return {value: this.alphabet.charAt(digitVal)};
} else {
return {done: true};
}
}
}).reverse().join(""));