Remove multiple positions in array [duplicate] - javascript

This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 3 years ago.
I have the following array
myArray = ["zell", "allen", 34, 223344, age , "Stree 45"]
i need to delete every position which be length less than 4
in this case the position 2 and 4
I wrote
for(var i=0; i<myArray.length; i++){
if(myArray[i].trim().length<3){
myArray.splice(i,1);
}
}
but works only with the first one, I need with every one
thanks

You can filter out the items that are shorter then 4 chars:
var myArray = ["zell", "allen", 34, 223344, "age" , "Stree 45"];
var filteredArr = myArray.filter(item => item.toString().length >= 4);
console.log(filteredArr);

Related

Array to multidimensional array [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 1 year ago.
I am trying to convert a simple array to a multidimensional array.
The Array I currently have
Array = (1, 2, 3, 4);
I would like to convert this Array to be like this
Array = ([1, 2], [3, 4]);
Any help is welcome, thanks already.
This worked for me:
function TwoDimensional(arr, size)
{
var res = [];
for(var i=0;i < arr.length;i = i+size)
res.push(arr.slice(i,i+size));
console.log(res);
}
Pretty easy when it works! Thanks

Changing one char in an array to another java [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 2 years ago.
It must be super simple, but I'm completely stuck.
var arr = ["ABCD", "asda12"];
var a = arr[0];
a[1] = 'S';
console.log(a[1]);
It logs "B" and I expect "S", why can't I do it this way?
In your code, "a" is a string and you can't change it's value in that way. You can try setting "a" as an array, then you can change it with the way you do:
var arr = ["ABCD", "asda12"];
var a = [...arr[0]];
a[1] = 'S';
console.log(a[1]);

How to find the number of repetitions of an item in an array in JavaScript? [duplicate]

This question already has answers here:
Idiomatically find the number of occurrences a given value has in an array
(11 answers)
Count the number of times a same value appears in a javascript array
(11 answers)
Closed 3 years ago.
how can I find the times that 1 (for example) is repeated in this array?
myArray = [1, 2, 3, 1, 4, 5, 1, 6];
Try this solution
function getOccurrence(myArray, value) {
var count = 0;
myArray .forEach((val) => (val === value && count++));
return count;
}

Selecting a random element from an array and displaying it on screen [duplicate]

This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 8 years ago.
I am trying to select a random element from an array and display it using jQuery. So far I can only click through the array and reset back to 0. Not sure how to use the Math.random(); exactly here.
Codepen: http://codepen.io/Travo100/pen/xAKji
counter = 0;
var compArray = [ "Cat", "Dog", "Rabbit", "Cow", "Sheep", "Human" ];
$('a').click(function () {
counter = (counter + 1) % compArray.length;
$(this).html(compArray[counter]);
});
Use this:
var rand = compArray[Math.floor(Math.random() * compArray.length)];

How can I compare two JavaScript arrays in order to determine if they have identical values? [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 8 years ago.
I don't want to compare the two arrays as a whole, I specifically want to check if the 2nd array has any values that match a value in the first one. I then want to return the value that matches in both.
If I'm not mistaken, comparing two arrays as a whole would be done like this:
array1 = [1,2,3];
array2 = [1,3,4];
console.log(JSON.encode(array1)==JSON.encode(array2));
So in this case, I would want to check if array2 has any matching values to array one, not if they both are equivalent. Any help is appreciated!
var array1 = [1, 2, 3],
array2 = [1, 3, 4];
var AnyItemsOfArray1InArray2 = array1.some(function(item) {
return array2.indexOf(item) > -1;
});
console.log(AnyItemsOfArray1InArray2);
var ItemsOfArray1InArray2 = array1.filter(function(item) {
return array2.indexOf(item) > -1;
});
console.log(ItemsOfArray1InArray2);

Categories

Resources