This question already has answers here:
How to skip over an element in .map()?
(18 answers)
Closed 4 years ago.
I have an array I want to increment over every other element. The 2nd element is always a repeat and I only want to process the first element. How can I get the map() method to increment by two and skip over one element?
let newArr = oldArr.map((item, i) => {
// process oldArr[0] item
// skips over oldArr[1] item
//...
});
You can use .filter to skip every second value:
let oldArr = [1,1,2,2,3,3,4,4,5,5];
let newArr = oldArr.filter((v,i) => i % 2).map((item, i) => {
console.log(item);
});
Related
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
In Javascript, how do I check if an array has duplicate values?
(9 answers)
Checking for duplicate strings in JavaScript array
(13 answers)
Closed 5 months ago.
I am writing a javascript function that takes a nested array and returns the numbers that occurs more than once in that array.
I believe my function is accurate (meaning that it passes their "Correctness test" ) but i am after efficiency, how efficient is this code?
For example - Lets call the name of the function deepSort(nestedArray) where nestedArray is the nested array parameter
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] == flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item, index) => results.indexOf(item) === index)).join()
}
const a = deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) // Returns 1,2,3,4,5
console.log(a);
const b = deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) // Returns 2
console.log(b);
const c = deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) // Returns 3,4,9
console.log(c);
Can this code be optimized any more for speed and efficiency when handling extremely large values of data?
This question already has answers here:
Does return stop a loop?
(7 answers)
Closed 9 months ago.
I somehow can't get my for loop inside of a function to print elements in an array.
could someone point out why javascript is not allowing the iteration to occur and only printing the first element?
const data1 = [17, 21, 23];
function printForecast(array) {
for (let i = 0; i < array.length; i++) {
let dayVsDays = i <= 1 ? 'day' : 'days'
return `The temp is ${array[i]}ÂșC in ${[i+1]} ${dayVsDays}`;
}
}
console.log(printForecast(data1))
Don't use return inside a loop unless you want to return the result of an specific iteration; this is why you only get the first iteration.
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 12 months ago.
How can i check if an array has element twice and log only the element that is not.
const duplicateElements = (array) => {
for(let numbers of array) {
// code here
}
}
const numbers = [1,3,2,4,1,3,2];
duplicateElements(numbers);
// Output 4
With the JS function filter you can archive this. First you have to iterate your array. then check with the filter function how many times the current value is inside your array. If equal 1 then push to an result array.
const d = [];
const arr = [1,3,2,4,1,3,2]
arr.forEach((e) => {
if (arr.filter(x => x == e).length === 1) {
d.push(e);
}
})
console.log(d);
This question already has answers here:
How to create an array containing 1...N
(77 answers)
Fastest way to fill an array with multiple value in JS. Can I pass a some pattern or function to method fill insted of a single value in JS? [duplicate]
(1 answer)
Closed 4 years ago.
I want to create a function that takes input from user and returns an array with all the numbers from 1 to the passed number as an argument. Example: createArray(10) should return [1,2,3,4,5,6,7,8,9,10]. I came up with this solution:
function createArray(input) {
var value = 0;
var array = [];
for (i=0;i<input;i++) {
value++;
array.push(value)
console.log(array)
}
}
createArray(12);
What is the correct and better way of doing it?
I would prefer to use Array.from:
const createArray = length => Array.from(
{ length },
// Mapper function: i is the current index in the length being iterated over:
(_, i) => i + 1
)
console.log(JSON.stringify(createArray(10)));
console.log(JSON.stringify(createArray(5)));
There is no need for the extra variable just do this:
function createArray(input) {
var array = [];
for (i = 0; i <= input; i++) {
array.push(i);
}
return array;
}
This question already has answers here:
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Closed 4 years ago.
What is the most efficient way to check a certain condition on every element of an array and return false if one or more elements do not meet the condition, for example, I have this array for example
arr = ["foo","azeaze", "wazeazerar"]
for(var ar in arr){
if(ar.length > 5){
console.log(false)
}else{
console.log(true)
}
}
as you can see it return true even if the element "foo" length is not greater than 5
You can use Array.prototype.every() in a one line function
arr = ["foo","azeaze", "wazeazerar"]
const isittrue = currentval => currentval.length > 2
console.log(arr.every(isittrue));
arr = ["foo","azeaze", "wazeazerar"]
console.log(arr.every(elem => elem.length >= 5))