How do I get the equivalent index of another array? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Here, I have two arrays.
let x = [0, 1, 2]
let y = [3, 4, 5]
What is the code so that when I input an index of array x, I get array y's equivalent? So like, I want to output 3 when I input x[0].

You can index y array with x elements (i is equals to x[i] in your case, is it wanted?) y[x[i]] or y[i]

If you insert a new item in array x using push operation ie. x.push(val), then corresponding element in array y will be:
y[x.length-1];

I have an array: y[x[i]]
but check for index range for array Y else you will get undefined.

Related

Javascript: check multiple elements in array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I have an array: a=[10,11,12,13]. How can check elements from a[0] to a[2] if they >0 in the shortest way and don't use for loop? Does anyone have any ideas? Thank you.
You can slice the array to get the items within the specified indexes, then use filter to filter out the items that don't match the condition and check whether the length of the resulting array is the length of the range.
const a = [10, 11, 12, 13]
const isValid = a.slice(0, 3).filter(e => e > 0).length == 3
//last index in slice is exclusive
console.log(isValid)

JS Function that checks a dynamic number of index positions and returns array element if it satisfies the condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been looking for a way to return elements in an array based on a dynamic number. This dynamic number will be the number of required index positions for each array element, if each element can satisfy the required number of index positions (its own position counts towards the requirement) then that element should be returned.
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
So with the variables listed above I should return [1,2,3,4] because 5 and 6 will not be able to return three index positions.
The Array.prototype.slice might give you some help. Please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
console.log(arr.slice(0, arr.length - requiredIndexes + 1))
I am not an expert but I think you would want to iterate up to the position. Are you having the dynamic number be a random number? You may want to also clone your array and use the clone as the worker array. Not sure exactly what you are wanting to do with the array, but you can then slice from the array and the parent array stays unefected.
function pickposition() {
var randIndex = Math.floor(Math.random() * clone.length);
rand = clone[randIndex];
clone.splice(randIndex,1);
}

How can i find the index of the element of an Array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Here is my code, it gives me undefined i have also tried indexof() method
let Numbers = [2,3,1,5,6,7,8 ];`
console.log("Unsorted array " + Numbers);
for(var i=0 ;i<Numbers.length;i++){`
alert(Numbers.findIndex[i]);
}
The below code would work after some changes in your code -
let Numbers = [2, 3, 1, 5, 6, 7, 8];
console.log("Unsorted array " + Numbers);
for (var i = 0; i < Numbers.length; i++) {
console.log(Numbers.indexOf(Numbers[i]));
}
In your case, you were using findIndex() which takes a function and executes it for each element of the array. You were passing it a number which is not correct. Also, the invocation of function you were doing was not correct - use () brackets and not [] brackets for function call.
Also, the i itself is the index. I don't know why you would need to use indexOf to get the index of element which you already know is present at a particular index. This method wouldn't be practical unless your array has duplicates and you need to find the first occurring index number for each element of array.
As a side tip, avoid using alert for such purposes. Stick with console log.

How to empty an Array of arrays in javaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array which is an array of arrays. I want to empty the array as in, get rid of all the arrays with in that array. How would I accomplish that? Thanks
I have tried setting it to new but that does not seem to work for me. I have also tried something like finalSetB.length = 0.
I get no errors but the array is just piling up and is slowing my next procedure down and not returning the expected results. Thanks for the help in advance.
Try array.splice. You can use it to remove elements from array. You can find documentation here.
var arr = [1, 2, 3, 4];
arr.splice(0, 4); // will remove 4 elements from 0th index
You could array.splice(0, array.length);
Why not just set the array to an empty array?
Just do:
array = [];

What is format of object JS? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
There is an array:
var data = data.addRows([
['Nitrogen', 0.78],
['Oxygen', 0.21],
['Other', 0.01]
]);
What does it mean? Arrays in array? What is it ['Nitrogen', 0.78] ? Why [] brackets?
I tried to reproduce this like:
var arr = [];
arr.push({'Oxygen', 0.21});
That is an array of arrays. Otherwise known as a multidimentional array. The [] inside the [] backets indicate a seperate arrays.
Pointy has already answered this in the comments but I will put it here.
arr.push({'Oxygen', 0.21}); is pushing an object to an array, which is denoted by the {} curly braces (However if you did want to push an object to an array the syntax would actually be arr.push({'Oxygen' : 0.21});). In order to push an array to array you would need to do arr.push(['Oxygen', 0.21]);, which is using [] square brackets.
yes it's array within an array; if you want to reproduce it should be like the following
var arr = [];
arr.push(['oxygen',0.21]);
// to see what's inside
console.log(JSON.stringify(arr));
// to check the type
for ( var i = 0 ; i < arr.length ; i++ ){
console.log('array[',i,'] = ', typeof(arr[i]));
}

Categories