I am trying to use the reduce method as follows to eliminate duplicates however, it is not quite working:
var unique = function(array) {
array = array.sort(function(a,b) {return a-b;});
var noDup = [array[0]];
array.reduce(function(c,d) {
if(c!==d) {
noDup.push(d);
return d;
}
});
return noDup;
};
var x = [9,2,1,5,9,1,1,4,2,9];//==>[1, 1, 2, 4, 5, 9, 9]
function unique(values) {
return values.reduce(function(prev, cur) {
if (prev.indexOf(cur) == -1) {
prev.push(cur);
}
return prev;
}, []);
}
unique([9,2,1,5,9,1,1,4,2,9]) // --> [9, 2, 1, 5, 4]
fiddle
You are using the "intermediate value" of reduce to hold the previous value, so you can check against it the next time through. But that leaves you with no way to calculate the real intermediate value you want, which is the unique array you are building, so you having to declare it outside (noDup), which sort of defeats the whole purpose. Then your code has issues such as not providing an initial value to reduce. In that case, reduce has a special behavior which is that it calls the callback with the first two values of the array; a situation you are not handling properly.
Anyway, since it seems you are willing to sort the array, you can avoid doing an indexOf each time through the loop, by just remembering the previous value and checking against it:
function unique(values) {
var prev;
return values . sort() . reduce(function(result, cur) {
if (cur !== prev) result.push(cur);
prev = cur;
return result;
}, []);
}
But it turns out actually we don't need to keep the value of prev; instead, we can simply refer to the previous element directly, since filter passes additional arguments of index and array to the callback, so:
function unique(values) {
return values . sort() . reduce(function(result, cur, index, array) {
if (cur !== array[index-1]) result.push(cur);
return result;
}, []);
}
But if you think about it, this is nothing more than a filter written using reduce. It's just filtering out numbers that are the same as the previous one. So just write it as a filter to start with:
function unique(values) {
return values . sort() . filter(value, i, arr) { return value !== arr[i-1]; });
}
There are other approaches to removing duplicates using filter which don't require the sort. Here's a simple one:
values . filter(function(value, i, arr) { return arr.indexOf(value) === i; });
What this says is, filter out a number if the location where it is first found in the array is its location. In other words, filter out numbers that occur earlier in the array.
Related
function twoSum(numbers, target) {
var result = [];
numbers.forEach(function(value, index) {
return numbers.forEach(function(value2, index2) {
if (value + value2 === target) {
result.push(index, index2);
return result;
}
})
})
return result;
}
twoSum([1, 2, 3], 4);
//Output - [ 0, 2, 1, 1, 2, 0 ]
Hi - I'm working on a particular codewars problem and I seem to be misunderstanding the usage of return for callback functions. In this particular problem I just want to find the first two sums of numbers that equal the target and push those index values into result. I don't want to keep iterating through my function after that - meaning I only want the first pair that's found. My current output gives me all the index values for the target sum. Not just the first 2. It seems I am not using my return commands correctly. My current line of thought is that return result returns a value to my nested callback of parameters (value2, index2). That result is then returned to my outside function of (value,index). Why does my loop not cease after that return?
It doesn't end because .forEach cannot be terminated early. forEach is not paying any attention to the values you return. If you want to terminate early you'll need to use a different approach.
If you want to stick with array methods, there are .some and .every. The former continues until a run of your function returns true, and the latter continues until a run of your function returns false. These are meant for doing compound OR's and compound AND's with every element of the array, but they can kinda be used for your case too.
numbers.some(function(value, index) {
return numbers.some(function(value2, index2) {
if (value + value2 === target) {
result.push(index, index2);
return true;
}
return false;
})
})
Or you could use a standard for loop, with the break keyword when you want to stop the loop.
Beside the not working return statement for a outer function, you need to take a different approach which uses only a single loop and an object for storing the index of the found value.
function twoSum(numbers, target) {
var indices = {};
for (let i = 0; i < numbers.length; i++) {
const number = numbers[i];
if (number in indices) return [indices[number], i];
indices[target - number] = i;
}
}
console.log(twoSum([1, 2, 3], 4));
I am trying to delete out the common elements in arguments given and
create a final array with unique elements.
In the below example my final array is giving me [1,3,4,5] instead of [1,4,5].
I did the debug and the one of element (3) is not going through the loop.
After element (2) it's skipping to element (4). I am confused why is this happening? Anything wrong with my code? Any help appreciated.
Thank you
function arrayBreaker(arr) {
let test = [];
test.push(arguments[1]);
test.push(arguments[2]);
let target = arguments[0];
target.filter(val => {
if (test.indexOf(val) > -1) {
target.splice(target.indexOf(val), 1);
}
});
return target;
}
console.log(arrayBreaker([1,2,3,4,5], 2, 3));
You're using .filter() like a general purpose iterator, and mutating the array you're iterating, which causes problems because the iterator is unaware of the items you're removing.
If you need to mutate the original array, instead return the result of the .indexOf() operation from the .filter() callback, or better yet, use .includes() instead, and then copy the result into the original after first clearing it.
function arrayBreaker(target, ...test) {
const res = target.filter(val => !test.includes(val));
target.length = 0;
target.push(...res);
return target;
}
console.log(arrayBreaker([1, 2, 3, 4, 5], 2, 3));
If you don't need to mutate, but can return a copy, then it's simpler.
function arrayBreaker(target, ...test) {
return target.filter(val => !test.includes(val));
}
console.log(arrayBreaker([1, 2, 3, 4, 5], 2, 3));
Array is returning a new array based on filter criteria, in this case you are checking index of test. In the callback function is expecting a true if you want to keep the value, otherwise return false. In your case you shouldn't slice the target inside filter callback, but rather:
function arrayBreaker(arr) {
let test = [];
test.push(arguments[1]);
test.push(arguments[2]);
let target = arguments[0];
let newArray = target.filter(val => {
if (test.indexOf(val) > -1) {
// filter value out
return false;
}
// keep the val inside new array
return true;
});
return newArray;
}
console.log(arrayBreaker([1,2,3,4,5], 2, 3));
for more information about array filter, check documentation here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Depending on your use case there are few things that I think would be helpful, first I would declare the function parameters so it's clear that arrayBreaker is expecting multiple arguments. Secondly I would expect second param as an array since we are expecting multiple values. Something like below:
function arrayBreaker(arr, filterValues) {
return arr.filter(val => {
if (filterValues.indexOf(val) > -1) return false;
return true;
})
}
console.log(arrayBreaker([1,2,3,4,5], [2, 3]));
I am trying to recreate the underscore js function difference using reduce.
Difference takes in multiple arrays and returns all values that are similiar to the first array. so [1,2],[2,3],[1,3] should spit out [1,2,2,1].
I was thinking of looping through each of my subarray(rest) and if the value of my sub elements has an index in my first array then I will push that element onto my accumulator value(which is an empty array).
Some reason I do not get my intended output where I was expecting [1,2,3,2,1,2].
instead I am getting [ [ 1, 2 ], [ 2, 4 ], [ 1, 2 ] ]. Can anyone help me with this rewrite using reduce. Thanks.
function difference(arrays){
var arrayed=Array.prototype.slice.call(arguments);
var first=arrayed[0];
var rest=arrayed.slice(1);
return reduce(first,function(acc,cur){
forEach(rest,function(sub){
if (sub.indexOf(cur)>-1){
acc.push(sub);
}});
return acc;
},[]);
}
console.log(difference([1,2,3],[2,4],[1,2],[4,5]));
i know the way im calling forEach is different but it is because my own version of forEach accepts two arguments.
As for your previous question, you need to call array methods on arrays, not pass them as parameters so not
return reduce(first,function(acc,cur){...})
but
return first.reduce(function(acc,cur){...});
In your function:
var first=arrayed[0];
var rest=arrayed.slice(1);
can be replaced with one step using splice:
var first = arrayed.splice(0, 1)
and use arrayed instead of rest. But there's no need for that anyway. If no accumulator is provided, then the first value is used, so (with some renaming of variables):
function difference(){
var args = Array.prototype.slice.call(arguments);
return args.reduce(function(acc, cur){
cur.forEach(function(value){
if (acc.includes(value)){
acc.push(value);
}
});
return acc;
});
}
console.log(difference([1,2,3],[2,4],[1,2],[4,5]));
Could try this
function difference(arrays){
var arrayed=Array.prototype.slice.call(arguments);
var first=arrayed[0];
var rest=arrayed.slice(1);
return first.concat(rest.reduce(function(acc, cur, curIndex, array){
first.forEach(function(eachEle){
if(cur.indexOf(eachEle) > -1 )
acc.push(cur[cur.indexOf(eachEle)]);
});
return acc;
}, []));
}
console.log(difference([1,2,3],[2,4],[1,2],[4,5]));
Here's a solution :
function difference(arrays){
var arrayed = Array.prototype.slice.call(arguments);
var first = arrayed.splice(0,1)[0]; // edit, thanks robG
var tmp = [];
return arrayed.reduce(function(acc, arr) {
return acc.concat(arr.filter(function(el) {
return first.includes(el);
}));
}, first);
}
console.log(difference([1,2,3],[2,4],[1,2],[4,5]));
Use the javascript reduce method on the 'rest' array, makes the first array be the default value of the accumulator, filter the arrays with the first one and concat the result to your accumulator.
Hope it helps,
best regards
I try to compare two strings in array on equal symbols or char,this code works, but how to implement it in ES6 with reduce method, if I have more than two strings an array. I need to return true if the string in the first element of the array contains all of the letters of the string in the second element of the array. But how to create the more flexible function if I have more than 2 elments in the array.
function mutation(arr) {
var arr2 = arr.map(item => item.toLowerCase().split(''));
for (i=0;i<arr2[1].length;i++) {
if (arr2[0].indexOf(arr2[1][i]) < 0)
return false;
}
return true;
}
mutation(["hello", "hey"]);
#Palaniichuk I thought your original algorithm was pretty solid. To handle your request I was able to create a solution that uses reduce.
I do have one question for you. Should the array increase in size, how would the strings be evaluated?
The reason I ask is because using a helper function like this might help you scale this algorithm. Of course it all depends on how the input changes. How the inputs are evaluated.
function makeStr(string) {
const reducer = string.split('').reduce((a, b) => {
a[b] = a[b] + 1 || 1;
return a;
}, {});
return Object.keys(reducer).sort().join('');
}
function secondMutation(arr) {
const array = [...arr].map(makeStr);
return array[0].includes(array[1]);
};
console.log(secondMutation(["hello", "hell"]));
I would like to filter an array of items by using the map() function. Here is a code snippet:
var filteredItems = items.map(function(item)
{
if( ...some condition... )
{
return item;
}
});
The problem is that filtered out items still uses space in the array and I would like to completely wipe them out.
Any idea?
EDIT: Thanks, I forgot about filter(), what I wanted is actually a filter() then a map().
EDIT2: Thanks for pointing that map() and filter() are not implemented in all browsers, although my specific code was not intended to run in a browser.
You should use the filter method rather than map unless you want to mutate the items in the array, in addition to filtering.
eg.
var filteredItems = items.filter(function(item)
{
return ...some condition...;
});
[Edit: Of course you could always do sourceArray.filter(...).map(...) to both filter and mutate]
Inspired by writing this answer, I ended up later expanding and writing a blog post going over this in careful detail. I recommend checking that out if you want to develop a deeper understanding of how to think about this problem--I try to explain it piece by piece, and also give a JSperf comparison at the end, going over speed considerations.
That said, **The tl;dr is this:
To accomplish what you're asking for (filtering and mapping within one function call), you would use Array.reduce()**.
However, the more readable and (less importantly) usually significantly faster2 approach is to just use filter and map chained together:
[1,2,3].filter(num => num > 2).map(num => num * 2)
What follows is a description of how Array.reduce() works, and how it can be used to accomplish filter and map in one iteration. Again, if this is too condensed, I highly recommend seeing the blog post linked above, which is a much more friendly intro with clear examples and progression.
You give reduce an argument that is a (usually anonymous) function.
That anonymous function takes two parameters--one (like the anonymous functions passed in to map/filter/forEach) is the iteratee to be operated on. There is another argument for the anonymous function passed to reduce, however, that those functions do not accept, and that is the value that will be passed along between function calls, often referred to as the memo.
Note that while Array.filter() takes only one argument (a function), Array.reduce() also takes an important (though optional) second argument: an initial value for 'memo' that will be passed into that anonymous function as its first argument, and subsequently can be mutated and passed along between function calls. (If it is not supplied, then 'memo' in the first anonymous function call will by default be the first iteratee, and the 'iteratee' argument will actually be the second value in the array)
In our case, we'll pass in an empty array to start, and then choose whether to inject our iteratee into our array or not based on our function--this is the filtering process.
Finally, we'll return our 'array in progress' on each anonymous function call, and reduce will take that return value and pass it as an argument (called memo) to its next function call.
This allows filter and map to happen in one iteration, cutting down our number of required iterations in half--just doing twice as much work each iteration, though, so nothing is really saved other than function calls, which are not so expensive in javascript.
For a more complete explanation, refer to MDN docs (or to my post referenced at the beginning of this answer).
Basic example of a Reduce call:
let array = [1,2,3];
const initialMemo = [];
array = array.reduce((memo, iteratee) => {
// if condition is our filter
if (iteratee > 1) {
// what happens inside the filter is the map
memo.push(iteratee * 2);
}
// this return value will be passed in as the 'memo' argument
// to the next call of this function, and this function will have
// every element passed into it at some point.
return memo;
}, initialMemo)
console.log(array) // [4,6], equivalent to [(2 * 2), (3 * 2)]
more succinct version:
[1,2,3].reduce((memo, value) => value > 1 ? memo.concat(value * 2) : memo, [])
Notice that the first iteratee was not greater than one, and so was filtered. Also note the initialMemo, named just to make its existence clear and draw attention to it. Once again, it is passed in as 'memo' to the first anonymous function call, and then the returned value of the anonymous function is passed in as the 'memo' argument to the next function.
Another example of the classic use case for memo would be returning the smallest or largest number in an array. Example:
[7,4,1,99,57,2,1,100].reduce((memo, val) => memo > val ? memo : val)
// ^this would return the largest number in the list.
An example of how to write your own reduce function (this often helps understanding functions like these, I find):
test_arr = [];
// we accept an anonymous function, and an optional 'initial memo' value.
test_arr.my_reducer = function(reduceFunc, initialMemo) {
// if we did not pass in a second argument, then our first memo value
// will be whatever is in index zero. (Otherwise, it will
// be that second argument.)
const initialMemoIsIndexZero = arguments.length < 2;
// here we use that logic to set the memo value accordingly.
let memo = initialMemoIsIndexZero ? this[0] : initialMemo;
// here we use that same boolean to decide whether the first
// value we pass in as iteratee is either the first or second
// element
const initialIteratee = initialMemoIsIndexZero ? 1 : 0;
for (var i = initialIteratee; i < this.length; i++) {
// memo is either the argument passed in above, or the
// first item in the list. initialIteratee is either the
// first item in the list, or the second item in the list.
memo = reduceFunc(memo, this[i]);
// or, more technically complete, give access to base array
// and index to the reducer as well:
// memo = reduceFunc(memo, this[i], i, this);
}
// after we've compressed the array into a single value,
// we return it.
return memo;
}
The real implementation allows access to things like the index, for example, but I hope this helps you get an uncomplicated feel for the gist of it.
That's not what map does. You really want Array.filter. Or if you really want to remove the elements from the original list, you're going to need to do it imperatively with a for loop.
Array Filter method
var arr = [1, 2, 3]
// ES5 syntax
arr = arr.filter(function(item){ return item != 3 })
// ES2015 syntax
arr = arr.filter(item => item != 3)
console.log( arr )
You must note however that the Array.filter is not supported in all browser so, you must to prototyped:
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
And doing so, you can prototype any method you may need.
TLDR: Use map (returning undefined when needed) and then filter.
First, I believe that a map + filter function is useful since you don't want to repeat a computation in both. Swift originally called this function flatMap but then renamed it to compactMap.
For example, if we don't have a compactMap function, we might end up with computation defined twice:
let array = [1, 2, 3, 4, 5, 6, 7, 8];
let mapped = array
.filter(x => {
let computation = x / 2 + 1;
let isIncluded = computation % 2 === 0;
return isIncluded;
})
.map(x => {
let computation = x / 2 + 1;
return `${x} is included because ${computation} is even`
})
// Output: [2 is included because 2 is even, 6 is included because 4 is even]
Thus compactMap would be useful to reduce duplicate code.
A really simple way to do something similar to compactMap is to:
Map on real values or undefined.
Filter out all the undefined values.
This of course relies on you never needing to return undefined values as part of your original map function.
Example:
let array = [1, 2, 3, 4, 5, 6, 7, 8];
let mapped = array
.map(x => {
let computation = x / 2 + 1;
let isIncluded = computation % 2 === 0;
if (isIncluded) {
return `${x} is included because ${computation} is even`
} else {
return undefined
}
})
.filter(x => typeof x !== "undefined")
I just wrote array intersection that correctly handles also duplicates
https://gist.github.com/gkucmierz/8ee04544fa842411f7553ef66ac2fcf0
// array intersection that correctly handles also duplicates
const intersection = (a1, a2) => {
const cnt = new Map();
a2.map(el => cnt[el] = el in cnt ? cnt[el] + 1 : 1);
return a1.filter(el => el in cnt && 0 < cnt[el]--);
};
const l = console.log;
l(intersection('1234'.split``, '3456'.split``)); // [ '3', '4' ]
l(intersection('12344'.split``, '3456'.split``)); // [ '3', '4' ]
l(intersection('1234'.split``, '33456'.split``)); // [ '3', '4' ]
l(intersection('12334'.split``, '33456'.split``)); // [ '3', '3', '4' ]
First you can use map and with chaining you can use filter
state.map(item => {
if(item.id === action.item.id){
return {
id : action.item.id,
name : item.name,
price: item.price,
quantity : item.quantity-1
}
}else{
return item;
}
}).filter(item => {
if(item.quantity <= 0){
return false;
}else{
return true;
}
});
following statement cleans object using map function.
var arraytoclean = [{v:65, toberemoved:"gronf"}, {v:12, toberemoved:null}, {v:4}];
arraytoclean.map((x,i)=>x.toberemoved=undefined);
console.dir(arraytoclean);