This question already has answers here:
How can the current number of i be accessed in a for of loop?
(6 answers)
Closed 6 years ago.
Lets say we have an array "myArray" and we want to iterate over it using the for..of. We are searching for a specific value and when we find it, we want to return the index where the value was. So, i have this:
var myArray=[1,2,3,4,5];
for (let item of myArray) {
if (item===3) {
//return index?
}
}
Is there any way to get the index? Thanks.
It does not come out of the box, but you may employ the new Array.prototype.entries(), which returns an iterator over the index-value pairs:
for (const [index, value] of myArray.entries()) {
// ...
}
Alternatively you could use the new Array.prototype.findIndex() which accepts a predicate and returns an index of the first element that matches it. Eg:
myArray.findIndex(v => v > 10); // would return an index of a first value
// that is greater than 10
Related
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);
This question already has answers here:
Finding items that appear only one time in a Javascript array
(5 answers)
Completely removing duplicate items from an array
(11 answers)
Closed 11 months ago.
let getNonDuplicates = [1,2,3,4,2,3,4,5]; // Here's my example array
the result I needed is to get 1 & 5 since those are the data that don't have duplicates
let resultMustBe = [1,5];
You could use filter method, to filter the array, based on the condition that index of an element from beginning is equal to index of the same element from the last. It means that the element is unique in the array.
let arr = [1,2,3,4,2,3,4,5];
let res = arr.filter(e=>arr.indexOf(e)===arr.lastIndexOf(e));
console.log(res);
This question already has answers here:
How can I only keep items of an array that match a certain condition?
(3 answers)
Closed 8 years ago.
I have a JS object/ associative array with some values:
var array =[
{val:1,ref:10,rule:100},
{val:1,ref:12,rule:120},
{val:2,ref:13,rule:165},
];
And I want to perform a .length, but want to be able to slice based on one of the keys (for instance val == 1). I want the length of values with val 1 rather than the length of the entire object. I have looked through the references material and could not find a satisfactory answer and I am unsure if this is feasible.
array.val==1.length = 2
Something like that...
You want to .filter the array for elements that match some predicate:
var filteredArray = array.filter(function(element) { return element.val == 1 })
filteredArray.length // = 2
filter applies the callback function to each element in the array, and the new filtered array contains all elements from original array for which the filter callback function returned true. Here, the function returns true for all elements with a val property of 1.
You need to use a .filter():
array.filter(function(v){return v.val==1}).length
This question already has answers here:
Counting the occurrences / frequency of array elements
(39 answers)
Closed 8 years ago.
I have an array with domain names without www in JavaScript. This array may contain duplicate records. I want to find all unique domains and their occurrence counts. Plz Help.
Here is the current code
exports.findUniqueDomains = function(uri_arr){
uri_arr.forEach(function(elem, index, arr){
arr[index] = elem.split('.')
.slice(-2)
.join('.');
});
return uri_arr.filter (function (v, i, a) { return a.indexOf (v) == i });
};
Make an object for keeping the values
Iterate through the array, each time inserting new entry / incrementing counter in the counting object
For iteration, you can use for...in, .forEach or some other method.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript array difference
Using jquery or javascript how do I compare 2 arrays so that if one array has elements within the other array these elements are eliminated from the other array?
You must cross the two array and compare each element of the first with each element of the second, then, use the Array.splice method to remove an element.
for (var i in array1) {
for (var j in array2) {
if (array2[j] == array1[i]) {
array2.splice(j, 1);
break;
}
}
}