Delete all the array items in JavaScript [duplicate] - javascript

This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 2 years ago.
How can I delete all the items from a JavaScript array?
example -
var myArray1, 2, 3, 4, 5, 6, 7, 8, 9];
// I want it to become this
myArray = [];

You can try this:
function clearArray(array) {
while (array.length) {
array.pop();
}
}

Related

How to subtract one array from another, in javascript [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
If I have an array A = [1, 2, 3, 4, 5] and B = [3, 4, 5] I want to return a new array with values
[1, 2]. remove same value.
Using Array.prototype.includes, you can check if B contains A item or not.
And using Array.prototype.filter, you can get the filtered values which are not included in array B.
const A = [1, 2, 3, 4, 5];
const B = [3, 4, 5];
const output = A.filter((item) => !B.includes(item));
console.log(output);

Remove duplicate elements and take one element from two array [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 2 years ago.
I have two arrays below,
let array_one = [1, 2, 3, 4, 5]; //first array
let array_two = [5, 6, 7, 8, 9]; //second array
//I need [1, 2, 3, 4, 5, 6, 7, 8, 9]
In array_one and array_two has one common element that is 5 but I need this 5 only one time. I don't need to remove the duplicate elements but need one single element.
Please try this, It should work for you
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
let arr = [...array_one,...array_two];
arr.filter(onlyUnique);

How can I iterate over nested arrays with recursion? [duplicate]

This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 3 years ago.
How can I iterate over nested arrays like this with recursion in javascript:
var a = [10, [1, [2,2,2], 3], 20, 'Hallo']
In Python it looks like this:
def foo1(L):
for i in L:
if not isinstance(i, list):
print(i)
else:
foo1(i)
foo1(a)
How can I write if not isinstance(i, list): in javascript?
You can use:
var a = [10, [1, [2,2,2], 3], 20, 'Hallo']
for (let item of a) {
if (!Array.isArray(item)){
console.log("Not array", item);
}else{
console.log("Array", item);
}
}

Getting Sum for all first items in each array, all second items in each array etc. ES6 [duplicate]

This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty

How to delete a elements in a array javascript? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(141 answers)
Closed 7 years ago.
I have an array in javascript.Example
var array=[1,2,3,4,5,2,6,7,2];
Ok, i want to delete value 2 in array. The result as
var array=[1,3,4,5,6,7];
Thank guys.
Use filter() for that
var array = [1, 2, 3, 4, 5, 2, 6, 7, 2];
array = array.filter(function(v) {
return v != 2
});
console.log(array);

Categories