How to double the array elements value - javascript

I have array elements and I am trying to multiply by 2 only using filter method (not map).The output I am expecting something looks like this [2,4,6,8,10].Here is my code
var array = [1,2,3,4,5];
array.filter( val => val * 2);

Not filter , you should use map function in this case.
var array = [1,2,3,4,5];
array = array.map(function(val){return val*2;});
console.log(array);
Demo

By using only the filter method... you could try using it as like an iterator (note that this is NOT how filter is supposed to be used):
var array = [1,2,3,4,5];
array.filter( function (val, i, array) { array[i] = val * 2; });
//array now has [2,4,6,8,10]
This is ugly, but this is what you would do if you can only use filter

Try using this
var new_array = array.map(function(e) {
e = e*2;
return e;
});

First of all you need to understand the functions of map, filter, and reduce.
From your requirement it should be function of map but you want with filter which is not obvious.
Map:
When you call map on an array, it executes that callback on every
element within it, returning a new array with all of the values
that the callback returned.
Filter
filter executes that callback on each element of the array, and spits
out a new array containing only the elements for which the callback returned true.
let items = ['1','2','3','4','5'];
let numbers = items.map(item=>item*2);
console.log(items);
console.log(numbers);

If you really want to use filter() you could to that:
Array.prototype.filter = Array.prototype.map
var array = [1,2,3,4,5];
array.filter( val => val * 2);
But please don't.

var numList = [1, 2, 3, 4, 5, 6, 7,8 ];
var doubleUp = (array) => array.map(item=>item*2);
console.log(doubleUp(numList));
//returns
[
2, 4, 6, 8,
10, 12, 14, 16
]

Related

map() method mutating the calling Array

map() can't mutate the calling array, instead it returns a new Array with modified values.
But, the following code mutating the original Array, is there any wrong in my understanding?
const arr = [1, 2, 3, 4, 5];
arr.map((num, index, arr1) => {
return arr1[index] = num * 2;
});
console.log(arr); // [2, 4, 6, 8, 10]
Well, you're mutating the original array by passing its reference into the callback function inside map() (arr1) and then manually accessing the indices. It will create a new array if you just return the value from that function.
const arr = [1, 2, 3, 4, 5];
const arr1 = arr.map((num) => {
return num * 2;
});
console.log(arr); // [1, 2, 3, 4, 5]
console.log(arr1); // [2, 4, 6, 8, 10]
The third argument to the callback function of map is the
original/source array on which the map is called upon
The arr and arr1 are both same i.e both are referencing on the same array, You can see it by using console.log(arr === arr1). So what ever you operation perform on the arr1, it gonna affect the arr.
const arr = [1, 2, 3, 4, 5];
arr.map((num, index, arr1) => {
console.log(arr1 === arr);
return num * 2;
});
You can just return num * 2 from the callback function. map internally creates a new array and return it. So you don't have to assign it as
arr1[index] = num * 2
You can also make it one-liner as:
arr.map((num, index, arr1) => num * 2)
const arr = [1, 2, 3, 4, 5];
const result = arr.map((num, index, arr1) => {
return num * 2;
});
console.log(arr); // [2, 4, 6, 8, 10]
console.log(result); // [2, 4, 6, 8, 10]
Array.map creates a new array populated with the results of calling a provided function on every element in the calling array.
Here its specifed that you must call or execute a function on every element of calling array.
What is the issue with your code?
You are not actually calling a function, you are instead updating the original array. If you are looking to create a new array by multiplying each node of the element with 2, you should do something like below.
Working Example
const arr = [1, 2, 3, 4, 5];
const newArray = arr.map((nodeFromOriginalArray, indexOfCurrentElement, arrayFromMapCalled) => {
return nodeFromOriginalArray * 2;
});
console.log(arr);
console.log(newArray);
Lets debug the paremeters inside the map function.
Here we have provided three arguments.
First argument nodeFromOriginalArray: The current element being processed in the array. This will be each node from your calling array.
Second argument indexOfCurrentElement: The index of the current element being processed in the array. Which means, the index of current element in calling array.
Third argument arrayFromMapCalled: The array map was called upon. This is the array on which the map function is getting executed. Please note, this is the original array. Updating properties inside this array results in updating your calling array. This is what happened in your case.
You should not modify your original array, which is the third parameter. Instead, you should return your node multipled by 2 inside map and assign this to a new array. Updating the third paramater inside the map function will mutate your calling array.
When calling map on an array, you provide a mapper with three arguments, an item in the array, it's index and the array itself (as you've represented in your snippet).
map takes the value returned by the function mapper as the element at the index in a new array returned by the operation.
const arr = [1,2,3,4,5]
const doubled = arr.map(x => x * 2) // [2,4,6,8, 10]
A over simplified implementation of map (without the index and originalArray params) might look like this. Let's assume that instead of being a method on the array instance, it's a function that takes an array and a mapper function.
I would not recommend re-implementing in production code, there's the native implementation as well as several libraries such as lodash and underscore that implement it.
function map(arr, mapper) {
const result = [];
for (const item of arr) {
const resultItem = mapper(item);
result.push(resultItem);
}
return result;
}
function double(x) {
return x * 2;
}
const doubled = map([1,2,3,4,5,6], double); // [2, 4, 6, 8 ,10, 12]

using array.map with repeat in Javascript

Hello I have the following:
var a = [1, 2, 3]
I would like to update this array to:
a = [11,22,33]
I am trying to do something like
a.map(repeat(2));
but it results in an error.
I know I could easily loop through this, but I am trying to practice using more concise code, and expand my knowledge a bit on different functions, and how to use them.
Is something like this possible?
You could convert the number to string, repeat the value and convert it back to number.
var a = [1, 2, 3]
a = a.map(a => +a.toString().repeat(2));
console.log(a);
Given that you seem to be looking for repeating the digits, use strings for that. Which have a handy repeat method for this purpose:
a.map(x => Number(String(x).repeat(2))));
If you want to use your notation, you need to make a higher-order repeat function that returns another function to be used as the map callback:
function repeat(times) {
return x => String(x).repeat(times);
}
a.map(repeat(2)).map(Number)
Yes that is possible. You can define repeat as a function that returns a function:
function repeat(times) {
return function (value) {
return +String(value).repeat(times);
}
}
// Your code:
var a = [1, 2, 3];
var result = a.map(repeat(2));
console.log(result);
The map method expects a function as argument, so the call to repeat(2) should return a function. That (inner) function uses String#repeat after converting the value to string, and then converts the result back to number with the unary +.
Here you go, number version and string version
const doubleIt = n => Number(`${n}${n}`);
const arr = [1, 2, 3, 55];
const strArr = arr.map(o => `${o}${o}`);
const numArr = arr.map(o => o * 11);
const numFromStringsArr = arr.map(o => doubleIt(o));
console.log(strArr);
console.log(numArr);
console.log(numFromStringsArr);
You can create an array of N .length with Array(), use Array.prototype.fill() to fill the created array with current element of .map() callback, chain Array.prototype.join() with "" as parameter, use + operator to convert string to number
var a = [1, 2, 3], n = 2;
a = a.map(el => +Array(n).fill(el).join(""));
console.log(a);
Simply, try it with Array join() method:
a.map(x => Array(2).join(x));

JavaScript - How to increase all the element in int array by 1

How to increase all the element in an int array by 1?
Ex:
make var a = [1,2,3,4] and increase all elements inside so that the result gives
a = [2,3,4,5]
Is there any method except doing a = a+[1,1,1,1]?
Nowadays it is being done with the arrow functions as following :)
console.log([1,2,3,4].map(v=> v+1));
Sure. Just use the JavaScript map function.
[1,2,3,4].map(function(entry) {
return entry+1;
});
As per MDN docs;
The map() method creates a new array with the results of calling a
provided function on every element in this array.
Another example of the map function in action provided by the MDN I added is;
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]
You can take advantage of the map() function, which will map each item within your array to a function that can be used to transform it:
[1,2,3,4].map(function(item) {
// Increment each item by 1
return item + 1;
});
Example
console.log([1, 2, 3, 4].map(function(item) {
return item + 1;
}));
You can use the map function
var a = [1,2,3,5];
var x = a.map(function(item){
return item+1;
})
console.log(x)
DEMO

Filter array based on an array of index

First I apologize if it's a duplicate (I searched but did not find this simple example...), but I want to select elements of arr1 based on the index in arr2:
arr1 = [33,66,77,8,99]
arr2 = [2,0,3]
I am using underscore.js but the 0 index is not retrieved (seems to be considered as false):
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return index;
}
});
Which returns:
# [77, 8]
How could I fix this, and is there a simpler way to filter using an array of indexes? I am expecting the following result:
# [77, 33, 8]
The simplest way is to use _.map on arr2, like this
console.log(_.map(arr2, function (item) {
return arr1[item];
}));
// [ 77, 33, 8 ]
Here, we iterate the indexes and fetching the corresponding values from arr1 and creating a new array.
Equivalent to the above, but perhaps a bit more advanced, is to use _.propertyOf instead of the anonymous function:
console.log(_.map(arr2, _.propertyOf(arr1)));
// [ 77, 33, 8 ]
If your environment supports ECMA Script 6's Arrow functions, then you can also do
console.log(_.map(arr2, (item) => arr1[item]));
// [ 77, 33, 8 ]
Moreover, you can use the native Array.protoype.map itself, if your target environment supports them, like this
console.log(arr2.map((item) => arr1[item]));
// [ 77, 33, 8 ]
for me the best way to do this is with filter.
let z=[10,11,12,13,14,15,16,17,18,19]
let x=[0,3,7]
z.filter((el,i)=>x.some(j => i === j))
//result
[10, 13, 17]
You are returning index, so in your case 0 treated as false. So you need to return true instead
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return true;
}
});
or just return _.contains()
res = _.filter(arr1, function(value, index){
return _.contains(arr2, index);
});
One can use the filter method on the array that one wants to subset. The filter iterates over the array and returns a new one consisting of the items that pass the test. The test is a callback function, in the example below an anonymous arrow function, that accepts required currentValue and optional index parameters. In the example below I used _ as the first parameter since it is not utilized and that way the linter does not highlight it as unused :).
Within the callback function the includes method of array is used on the array that we use as the source of indices to check whether the current index of the arr1 is part of the desired indices.
let arr1 = [33, 66, 77, 8, 99];
let arr2 = [2, 0, 3];
let output = arr1.filter((_, index) => arr2.includes(index));
console.log("output", output);
_.contains returns a boolean. You should return that from the filter predicate, rather than the index because 0 is a falsy value.
res = _.filter(arr1, function(value, index)) {
return _.contains(arr2, index);
});
As an aside, JavaScript arrays have a native filter method so you could use:
res = arr1.filter(function(value, index)) {
return _.contains(arr2, index);
});
Isn't it better to iterate through indices array as main loop?
var arr1 = [33,66,77,8,99]
var arr2 = [2,0,3]
var result = [];
for(var i=0; i<arr2.length; i++) {
var index = arr2[i];
result.push(arr1[index]);
}
console.log(result);

Duplicate an array an arbitrary number of times (javascript)

Let's say I'm given an array. The length of this array is 3, and has 3 elements:
var array = ['1','2','3'];
Eventually I will need to check if this array is equal to an array with the same elements, but just twice now. My new array is:
var newArray = ['1','2','3','1','2','3'];
I know I can use array.splice() to duplicate an array, but how can I duplicate it an unknown amount of times? Basically what I want is something that would have the effect of
var dupeArray = array*2;
const duplicateArr = (arr, times) =>
Array(times)
.fill([...arr])
.reduce((a, b) => a.concat(b));
This should work. It creates a new array with a size of how many times you want to duplicate it. It fills it with copies of the array. Then it uses reduce to join all the arrays into a single array.
The simplest solution is often the best one:
function replicate(arr, times) {
var al = arr.length,
rl = al*times,
res = new Array(rl);
for (var i=0; i<rl; i++)
res[i] = arr[i % al];
return res;
}
(or use nested loops such as #UsamaNorman).
However, if you want to be clever, you also can repeatedly concat the array to itself:
function replicate(arr, times) {
for (var parts = []; times > 0; times >>= 1) {
if (times & 1)
parts.push(arr);
arr = arr.concat(arr);
}
return Array.prototype.concat.apply([], parts);
}
Basic but worked for me.
var num = 2;
while(num>0){
array = array.concat(array);
num--}
Here's a fairly concise, non-recursive way of replicating an array an arbitrary number of times:
function replicateArray(array, n) {
// Create an array of size "n" with undefined values
var arrays = Array.apply(null, new Array(n));
// Replace each "undefined" with our array, resulting in an array of n copies of our array
arrays = arrays.map(function() { return array });
// Flatten our array of arrays
return [].concat.apply([], arrays);
}
console.log(replicateArray([1,2,3],4)); // output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
What's going on?
The first two lines use apply and map to create an array of "n" copies of your array.
The last line uses apply to flatten our recently generated array of arrays.
Seriously though, what's going on?
If you haven't used apply or map, the code might be confusing.
The first piece of magic sauce here is the use of apply() which makes it possible to either pass an array to a function as though it were a parameter list.
Apply uses three pieces of information: x.apply(y,z)
x is the function being called
y is the object that the function is being called on (if null, it uses global)
z is the parameter list
Put in terms of code, it translates to: y.x(z[0], z[1], z[2],...)
For example
var arrays = Array.apply(null, new Array(n));
is the same as writing
var arrays = Array(undefined,undefined,undefined,... /*Repeat N Times*/);
The second piece of magic is the use of map() which calls a function for each element of an array and creates a list of return values.
This uses two pieces of information: x.map(y)
x is an array
y is a function to be invoked on each element of the array
For example
var returnArray = [1,2,3].map(function(x) {return x + 1;});
would create the array [2,3,4]
In our case we passed in a function which always returns a static value (the array we want to duplicate) which means the result of this map is a list of n copies of our array.
You can do:
var array = ['1','2','3'];
function nplicate(times, array){
//Times = 2, then concat 1 time to duplicate. Times = 3, then concat 2 times for duplicate. Etc.
times = times -1;
var result = array;
while(times > 0){
result = result.concat(array);
times--;
}
return result;
}
console.log(nplicate(2,array));
You concat the same array n times.
Use concat function and some logic: http://www.w3schools.com/jsref/jsref_concat_array.asp
Keep it short and sweet
function repeat(a, n, r) {
return !n ? r : repeat(a, --n, (r||[]).concat(a));
}
console.log(repeat([1,2,3], 4)); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
http://jsfiddle.net/fLo3uubk/
if you are inside a loop you can verify the current loop index with the array length and then multiply it's content.
let arr = [1, 2, 3];
if(currentIndex > arr.length){
//if your using a loop, make sure to keep arr at a level that it won't reset each loop
arr.push(...arr);
}
Full Example:
https://jsfiddle.net/5k28yq0L/
I think you will have to write your own function, try this:
function dupArray(var n,var arr){
var newArr=[];
for(var j=0;j<n;j++)
for(var i=0;i<arr.length;i++){
newArr.push(arr[i]);
}
return newArr;
}
A rather crude solution for checking that it duplicates...
You could check for a variation of the length using modulus:
Then if it might be, loop over the contents and compare each value until done. If at any point it doesn't match before ending, then it either didn't repeat or stopped repeating before the end.
if (array2.length % array1.length == 0){
// It might be a dupe
for (var i in array2){
if (i != array1[array2.length % indexOf(i)]) { // Not Repeating }
}
}

Categories

Resources