How to empty an Array of arrays in javaScript? [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 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 = [];

Related

Why array is not showing proper result in react? [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 19 days ago.
Improve this question
Below is the screen of my VScode, I am declaring arr1 as an array of numbers and later reversing it.Input
The problem is that on output the 1st paragraph is showing the reversed arrays instead of original one. What is the exact problem?
Output
Try this:
let reverse = Array.from(arr1).reverse()
As reverse() method modifies the original array (arr1), then you need to create a new array with Array.from(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Reverse is a mutating method, so both arr1 and reverse reference the same array. You'll want to do something like const reverse = [...arr1].reverse() instead to ensure that you aren't mutating the original array.

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 do I get the equivalent index of another 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, 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.

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

Get the minimum value from the 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 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.

Categories