find first number in array by js [duplicate] - javascript

This question already has answers here:
How to find the array index with a value?
(12 answers)
Find first non-zero value index in an array
(1 answer)
How to find first element of array matching a boolean condition in JavaScript?
(14 answers)
Closed 6 months ago.
I have some arrays as below and I want to know how to get the index of the first number by js?
Arr1=[null,null,null,null,null,...,343,959,543,..,252,null,null,...,null]

You can use findIndex:
Arr1.findIndex(value => value !== null)

You can loop through the array and check it's value:
const Arr1=[null,null,null,null,null,343,959,543,252,null,null,null];
let index = -1;
for(let i = 0; i < Arr1.length; i++)
{
if (Arr1[i] !== null)
{
index = i;
break;
}
}
console.log(index);

Related

Is there .count method in javascript like python? [duplicate]

This question already has answers here:
Idiomatically find the number of occurrences a given value has in an array
(11 answers)
How to count certain elements in array?
(27 answers)
Closed 2 years ago.
In Python, if we have a list,
array = [1,2,3,4,5]
If we say array.count(1), it will return the count of 1 in array.
Is there a method like this in javascript?
I think there isn't. But you can make one by using prototype.
let array = [1,2,3,4,5,1];
Array.prototype.count = function(value) {
let count = 0;
this.forEach(item => {
if (item === value) {
count++;
}
});
return count;
}
console.log(array.count(1));
console.log(array.count(2));

How can I find match string in array javascript? [duplicate]

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Javascript Searching Array for partial string
(2 answers)
Check for Partial Match in an Array
(5 answers)
Closed 3 years ago.
How can I find a match in an array of strings using Javascript? For example:
var str = "https://exmaple.com/u/xxxx?xx=x";
var filter = ["/u","/p"];
if (!str.includes(filter)){
return 1;
}
In the above code I want to search for var str if match this two values of array filter
This should work:
var str = "https://exmaple.com/u/xxxx?xx=x";
var filters = ["/u","/p"];
for (const filter of filters) {
if (str.includes(filter)) {
console.log('matching filter:', filter);
break; // return 1; if needed
}
}
In your array you need to find the element which includes "/u". filter will return an array and will contain only those element which includes u
var x = ["https://exmaple.com/u/xxxx?xx=x", "https://exmaple.com/p/xxxx?xx=x"];
let matched = x.filter(item => item.includes("/u"));
console.log(matched)
Try this:
var x = ["https://exmaple.com/u/xxxx?xx=x","https://exmaple.com/p/xxxx?xx=x"], i;
for (i = 0; i < x.length; i++) {
if(x[i].includes('/u')) {
document.write(x[i]);
}
}
This loops through all the URLs and picks the one that has /u in it. Then it just prints that out.

How to immutably insert into a sorted array? [duplicate]

This question already has answers here:
Efficient way to insert a number into a sorted array of numbers?
(18 answers)
Closed 3 years ago.
How do I immutably insert an element into a sorted array? (Let's assume an array of integers for simplicity)
The reason for this question: I'm writing a reducer for a React app, where the order of elements in my particular array is important.
The closest solution I've found is this one here, but it doesn't cover insertions into a sorted array.
Try this one.
let sortedArr = [1,2,5,9,12];
const newItem = 7;
for (let i = 0; i < sortedArr.length; i++) {
if (newItem <= sortedArr[i]) {
sortedArr = [...sortedArr.slice(0, i), newItem, ...sortedArr.slice(i)];
break;
}
}
console.log(sortedArr);

In an array, how to make sure the elements don't repeat, in javascript? [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 4 years ago.
I have this code, where the point is to read lottery numbers, and the numbers can not be smaller than 0, nor bigger than 49, nor can they repeat themselves. I don't understand how to put that in the while loop. How can I compare each number is inserted with the numbers previously inserted?
var totoloto=new Array(1);
for(var i=0; i<1; i++) {
totoloto[i]=new Array(5);
for(var j=0; j<5; j++) {
do {
totoloto[i][j] = parseInt(readLine("totoloto="));
} while (totoloto[i][j] < 1 || totoloto[i][j] > 49);
}
print(totoloto[i].toString().replaceAll(",", " "));
}
You can use Set instead of Array and just add values into set. If you don't know, Set contains only unique values.
Another way to do this is just using object:
let obj={}
obj.value1 = true // true is just for example. It doesn't make any sense
obj.value2 = true
// After getting all the keys you can
Object.keys(obj) // it returns all the keys in objecti i.e. your values
So, adding values with the same keys has no effect, because object can have only unique keys.

How to remove all the elements from array [duplicate]

This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 6 years ago.
I have an array eg: a[3,4,5,6,7,8]. I want to remove all the element in one time and make array as an empty array. How to remove all the element of an array.
My code
var a = [2,3,4,5,6];
for(var i=0; I<a.length; i++){
a.remove();
}
a.length = 0;
This is all what you need
var a = [2,3,4,5,6];
console.log(a);
a.length = 0;
console.log(a);
Do a.length = 0; if you don't want to lose references. Do a = []; if you want to lose references.

Categories

Resources