How to get index in forEach? [duplicate] - javascript

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

Related

Returning the full contents of an array [duplicate]

This question already has answers here:
Does return stop a loop?
(7 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
function monkeyCount(n) {
for (i=1; i<=n; ++i){
let monkeyArray=[i];
return monkeyArray[i];
}
}
Another rookie question lol. I need to return the values of an entire array using the return statement and not the console.log. If I pass a number such as 5 to the function I need to return 1,2,3,4,5 your help much appreciated:0)
It looks like you want to append values to the array not reassign the array at every iteration of the loop. Try this:
const monkeyArray = [];
for(let i = 1; i<= n; i++){
monkeyArray.push(i);
}
return monkeyArray;
There are also many ways to do this such as with the lodash library where you can just call _.range(1, 6) to get an array from [1,6)
This is pretty simple, you just have try this on browser console.
function monkeyCount(n) {
let monkeyArray = [];
for (i=1; i<=n; ++i) {
monkeyArray[i-1] = i;
}
return monkeyArray.join(',');
}

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 to print the key and value of an object? [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 5 years ago.
I have an object like this
var data = {'name':'test','rollnum':'3','class':'10'};
I want to console it by iterating through it like,
name:test
rollnum:3
class:10
Can anyone please help me.Thanks.
This will work for values that are Strings or Numbers.
var data = {'name':'test','rollnum':'3','class':'10'};
var i;
for (i in data) {
console.log(i + ":" + data[i]);
}
With modern JavaScript syntax, this becomes quite elegant:
const data = {'name':'test','rollnum':'3','class':'10'};
Object.entries(data).forEach(([key, val]) => console.log(`${key}: ${val}`));
for(i in data) {
console.log (i,':', data[i])
}

Remove duplicates from array within array in Javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 7 years ago.
I started with an array formatted like this:
var cus = {
"acct":[
{
"latitude":"41.4903",
"longitude":"-90.56956",
"part_no":"P1140",
"no_sold":1
},
{
"latitude":"48.118625",
"longitude":"-96.1793",
"part_no":"227",
"no_sold":1
},
....
]
Next I put all of the part_no in a separate array like this:
var list = [];
$.each(cus.acct,function(index,value){
list = [value["part_no"]];
These are the results when I do a console.log() of my array:
["P1140"]
["227"]
["224"]
["600"]
.....
["756"]
["756"]
["756"]
How do I remove duplicates from this array of just part_no's with javascript/jquery? I've looked at other examples but can't find one that works for me. Take note that I'm just beginning with javascript as well.
function getUnique(arr){
var result = [];
$.each(arr, function(i, e) {
if(typeof e != "undefined")
{
if ($.inArray(e, result) == -1) result.push(e)
}
});
return result;
}
If you can use any libraries like underscope or lodash will provide more options.
I would use the jQuery unique option. It should remove any duplicates from your array.

check if xy[i] matchs any element of an array without forloop [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
array.contains(obj) in JavaScript
Finding an item of an array of numbers without using a loop
i'm wondering if its possible to check if xy[i] matchs any element of an array without using for loop in JavaScript? . with example, please?
thanks
You can either use a loop or use a function that uses a loop.
Some libraries, like jQuery, offer their own functions (that use loops).
Using a loop is not a bad thing.
You can keep your code pretty (and organized) by putting the loop in a function.
var stringArray = [ "one", "two", "three" ];
var searchTerm = "two";
if (contains(stringArray, searchTerm)) {
alert("found it");
}
function contains(someArray, someTerm) {
for (var i = 0; i < someArray.length; i++) {
if (someArray[i] === someTerm) {
return true;
}
return false;
}

Categories

Resources