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

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

Related

find first number in array by js [duplicate]

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

Remove array elements matching element data [duplicate]

This question already has answers here:
remove objects from array by object property
(15 answers)
Closed 2 years ago.
I have
stone = [];
stoneB.playerId = players[player.playerId].playerId
I push new elements into stone by
stone.push(stoneB)
How would I go about removing all of the elements from stone that match stoneB.playerId for a given player?
.filter is probably your best bet here:
stone = stone.filter(d => d.playerId !== stoneB.playerId)
or more generally:
function remove(stone, playerId) {
return stone.filter(d => d.playerId !== playerId);
}
stone = remove(stone, stoneB.playerId);

String to array js [duplicate]

This question already has an answer here:
Split string into array of equal length strings
(1 answer)
Closed 2 years ago.
I want to convert a String '1234567' to Array
like this['123','456','7']
i have try this:
let str='1234'
let num=3;
let temp='';
let array=str.split('')
let newArr=[];
for(let i = 0;i<array.length;i++){
if((i+1)%num!==0){
temp+=array[i]
}else{
temp+=array[i]
newArr.push(temp)
temp='';
}
}
console.log(newArr)
but it miss the 4
When the loop finishes, temp may be non-empty without having been added to newArr. One way to handle this would be to check for this scenario after the loop and handle accordingly.
// after the for-loop
if (temp) {
newArr.push(temp)
}

How to get index in forEach? [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 2 years ago.
I'm inheriting some code from someone else but I never used this way. I used to use
for(var i = 0; i<items.length; ++i; {
items[i];
Or
myArray.forEach(function (value, i) {
items[i];
But what if I use the following?
filtererdData.forEach(regionData => {
index?
Something like this.
filteredData.forEach((regionData, index) => {
// your code here
})
Ref forEach: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Finding values of a mapping/dict [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 9 years ago.
I have map/dictionary in Javascript:
var m = {
dog: "Pluto",
duck: "Donald"
};
I know how to get the keys with Object.keys(m), but how to get the values of the Object?
You just iterate over the keys and retrieve each value:
var values = [];
for (var key in m) {
values.push(m[key]);
}
// values == ["Pluto", "Donald"]
There is no similar function for that but you can use:
var v = Object.keys(m).map(function(key){
return m[key];
});

Categories

Resources