How do I use reduce() to mimic the capabilities of forEach()? - javascript

I'm trying to accomplish a task with reduce() that I know how to do with forEach(). Unfortunately I'm not sure how to structure the syntax and I can't find a good example.
Example 1) In this code I use forEach() to insert the word species into the first index of each array.
"use strict";
var creatureArray;
creatureArray = [
['zombie', 30, 1, 'bite', 0, 5],
['skeleton', 10, 2, 'sword', 1, 10],
['orc', 15, 4, 'club', 1, 7]
];
creatureArray.forEach(function(value, index, array) {
array[index].unshift('species');
});
console.log(creatureArray);
Example 2) In this code I try to accomplish something similar using .reduce(). However I know that I'm missing a piece of syntax. I can't figure out how to apply the updated array to the accumulator which is then returned as an object. Thanks so much for any help!
"use strict";
var creatureArray, creatureObject;
creatureArray = [
['zombie', 30, 1, 'bite', 0, 5],
['skeleton', 10, 2, 'sword', 1, 10],
['orc', 15, 4, 'club', 1, 7]
];
creatureObject = creatureArray.reduce(function(accumulator, currentValue, index, array) {
array[index].unshift('species');
//what goes here?
return accumulator;
}, {});
console.log(creatureObject);

Here is how you can do accomplish it.
"use strict";
var creatureArray, creatureObject;
creatureArray = [
['zombie', 30, 1, 'bite', 0, 5],
['skeleton', 10, 2, 'sword', 1, 10],
['orc', 15, 4, 'club', 1, 7]
];
creatureObject = creatureArray.reduce((accumulator, currentValue) => {
return [...accumulator, ['species', ...currentValue]];
}, []);
console.log(creatureObject);
The ... syntax above is called the Spread Operator.
When applied it expands the elements of the array into the new array created by the enclosing [ and ], placing each element from the old array as a top level element into the new array. This results in a flat array instead of a nested array. E.g [[1, 2]] -> [[1, 2]], but [...[1, 2]] -> [1, 2].
This is highly useful because it enables both simple concatenation as well as insertion of top level elements either before or after the expanded array.
Consider:
"use strict";
const names = ['Linus', 'Jane', 'David'];
const withJakeAppended = [...names, 'Jake'];
const withJakePrepended = ['Jake', ...names];
[names, withJakeAppended, withJakePrepended].forEach(xs => console.log(xs));
As you can see, when we spread an Array it is not modified, so the pleasant syntax also enables improved ergonomics for immutable, value oriented programming.
Even better, ... works with Sets and Maps as well. In fact, it works any Iterable object, including ones we can create ourselves.
I might add that using the fourth argument to either Array.prototype.forEach or Array.prototype.forEach is a poor practice that should be avoided.
Note if you need to do this in a browser without ES2015 support, you can write
"use strict";
var creatureArray, creatureObject;
creatureArray = [
['zombie', 30, 1, 'bite', 0, 5],
['skeleton', 10, 2, 'sword', 1, 10],
['orc', 15, 4, 'club', 1, 7]
];
creatureObject = creatureArray.reduce(function (accumulator, currentValue) {
return accumulator.concat([['species'].concat(currentValue)]);
}, []);
console.log(creatureObject);

map is more apropriate in this case:
var creatureArray = [ ['zombie' , 30, 1, 'bite' , 0, 5],
['skeleton', 10, 2, 'sword', 1, 10],
['orc' , 15, 4, 'club' , 1, 7] ]
var creatureObject = creatureArray.map(currentValue => ['species', ...currentValue] )
console.log( JSON.stringify( creatureObject ).replace(/],/g, '],\n ') )

Related

For two arrays find items that are present in one array only (symmetric difference)

I need to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
My algorithm consists of using the map() method on the first array and compare each of element of that array with the elements of the second array using every(). If this method returns true, the element gets returned on the block level of map (which will eventually add it to the returned array) if not it's discarded.
I'm not sure why my code is not working. This is an example of a wrong output using my code:
function diffArray(arr1, arr2) {
var newArr = arr1
.map(elem1 => {
if (arr2.every(elem2 => elem2 != elem1)) {
return elem1;
}
});
return newArr;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
This is the wrong output:
[ undefined, undefined, undefined, undefined ]
The expected output is : [4]
Your approach iterates the first array and because of using map along with the check for the value, you get undefined for every element of arr1.
If you take filter and the other array as well, you could get the wanted result.
function diffArray(arr1, arr2) {
return [
...arr1.filter(elem1 => arr2.every(elem2 => elem2 != elem1)),
...arr2.filter(elem1 => arr1.every(elem2 => elem2 != elem1))
];
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
Another approach takes an array of all values of both arrays and filter by checking if the value is not included in both arrays.
function diffArray(arr1, arr2) {
return [...arr1, ...arr2].filter(v => arr1.includes(v) !== arr2.includes(v));
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
Use _.difference(array, [values])
from lodash
or
own solution :
const diffArray = (arrayA, arrayB) => {
const output = []
const setA = new Set(arrayA);
arrayB.forEach((n) =>{
if(!setA.has(n)){
output.push(n)
}
})
const setB = new Set(arrayB);
arrayA.forEach(n =>{
if(!setB.has(n)){
output.push(n)
}
})
return output;
}
console.log(diffArray([1, 2, 3, 5, 6], [1, 2, 3, 4, 5])); //4, 6
This algorithm works even when numbers appear multiple times in both arrays.
It uses a Map created from the items of both arrays. The map contains the item as the key, and the value is the amount of times it's found in the 1st array - the number of times it's found it the 2nd array.
After creating the Map, it's converted to an array of [item, count]. The array is then filtered, and all items with a count of 0 are removed (they exist equally in both arrays), and then we map the array to an array of items.
const getCounts = (arr, init = new Map(), inc = 1) =>
arr.reduce((acc, item) => acc.set(item, (acc.get(item) || 0) + inc), init);
function diffArray(arr1, arr2) {
// create a Map that adds 1 for all items in arr1, and substructs 1 for every item in arr2
const counts = getCounts(arr2, getCounts(arr1), -1);
// convert to an array of pairs [item, count]
return Array.from(counts)
.filter(([, v]) => v) // remove all items with count 0
.map(([k]) => k); // map to the original item
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
console.log(diffArray([1, 2, 3, 3, 5], [1, 2, 3, 4, 5]));
console.log(diffArray([5, 1, 2, 3, 5], [1, 2, 3, 4, 5, 5]));
console.log(diffArray([1, 1, 2, 2, 3, 3, 5], [1, 2, 2, 3, 4, 5]));
Your function returns elements of the first array that are not present in the second array.
Its return is explained by the fact that .map() returns the array of exact same size as your input (arr1), but since all items of arr1 are present in arr2 you don't enter if(-statement body hence undefined is getting returned.
If your intention was to return items that are present in one array only (regardless of the order they're passed in), you may leverage the Map object together with Array.prototype.reduce():
combine arrays-arguments into common array of arrays
loop through those inner arrays with .reduce(), building up the Map, showing how many times each item is seen within combined array
for each item of combined array remove duplicates and increment respective counter
spread resulting Map into .entries() and .filter() those to find out uniques
const arr1 = [1, 2, 3, 5],
arr2 = [1, 2, 3, 4, 5],
getUniques = (...arrays) =>
[...arrays
.reduce((acc, arr) => {
[...new Set(arr)]
.forEach(item =>
acc.set(item, (acc.get(item)||0)+1))
return acc
}, new Map)
.entries()]
.reduce((acc, [item, repetitions]) =>
(repetitions == 1 && acc.push(item), acc), [])
console.log(getUniques(arr1, arr2))
.as-console-wrapper {min-height:100%;}
Above approach is of O(n)-time complexity, as opposed to your initial attempt and the answer you have currently accepted (both having O(n²)-time complexity). As a result it may perform much faster on large arrays (arbitrary number of arrays, as a bonus).

What does it mean to write two arrays together?

const theme = createMuiTheme({
spacing: factor => [0, 4, 8, 16, 32, 64][factor],
});
theme.spacing(2); // = 8
This is a sample code in the Material-UI framework document.
Here is the original link to the code:https://material-ui.com/customization/spacing/
[factor] is not an array or a list, it's to get the element from the [0, 4, 8, 16, 32, 64] as an argument, like in this code
const theme = createMuiTheme({
spacing: factor => [0, 4, 8, 16, 32, 64][factor],
});
theme.spacing(2); // = 8
The spacing is function and the factor is parameter / argument
You only have one array in your code block. If you've used arrays before you should be familiar with how you can access indexes within the array to get particular elements:
// 0 1 2 2 3 4
const arr = [0, 4, 8, 16, 32, 64];
const eight = arr[2];
console.log(eight);
Above, I'm using bracket notation ([2]) which allows you to access a given element within the array. We can rewrite the above code to use the literal array [0, 4, 8, 16, 32, 41] in place of the arr variable like so:
const factor = 2; // make 2 a variable called `factor`
const eight = [0, 4, 8, 16, 32, 64][factor];
console.log(eight);
As you can see, [factor] is being used to get a particular value (using bracket notation) from the array [0, 4, 8, 16, 32, 64].

Problem with Mocha test when try to use the 'return' in the function

I am learning the Mocha test for js but have a weird problem an couldn't figure it out. Any big man can help me:
I do this test case:
it("remove all the number larger than 10", () => {
function filter(arr) {
return arr.filter(number => number < 11);
}
assert.equal(filter([1, 3, 3, 5, 10, 29, 3], [1, 3, 3, 5, 10, 3]));
});
But it returns undefined for that filter function, but when I remove the return keyword, it works fine:
it("remove all the number larger than 10", () => {
function filter(arr) {
arr.filter(number => number < 11);
}
assert.equal(filter([1, 3, 3, 5, 10, 29, 3], [1, 3, 3, 5, 10, 3]));
});
Can anyone can explain it to me?
Thanks
You've got a typo, your close paren ) for filter should be immediately after the first array instead of after both arrays.
Also, to compare arrays use assert.deepEqual instead of assert.equal:
it("remove all the number larger than 10", () => {
function filter(arr) {
return arr.filter(number => number < 11);
}
assert.deepEqual(filter([1, 3, 3, 5, 10, 29, 3]), [1, 3, 3, 5, 10, 3]); // SUCCESS
});
(The reason why it was passing when you removed the return keyword is that filter was being passed both arrays, then returning undefined. assert.equal was only being called with one argument so its second argument was implicitly undefined. Since undefined == undefined your test passed.)

Sonnification javascript only returning a single note

I have been trying to use a node.js script to turn some data into music. The script is only returning a single note for some reason:
The orignal script on github: https://github.com/wbkd/from-data-to-sound had res.concat(scribble.scale('c', but the threw an error Invalid Scale name.
const scribble = require('scribbletune');
// example data
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
const min = Math.min(...data);
const octaves = [...Array(5)].map((d, i) => i + 1); // [1, 2, 3, 4, 5]
// creates array of notes like 'c1', 'd1', 'e1', 'gb1', 'ab1', 'bb1', 'c2', ...
const notes = octaves.reduce((res, octave) =>
res.concat(scribble.scale('c1 major', 'whole tone', octave, false))
, []);
const midiData = scribble.clip({
notes: data.map(value => notes[value - min]),
pattern: 'x',
noteLength: '1/16',
});
// write the MIDI file 🎵🎵🎵
scribble.midi(midiData, 'data-sonification.mid');
From scribbletune doc:
each x implies a note on event
scribbletune docs/core/clip
Since you're passing only 1 'x' as a pattern in scribble.clip, it only plays 1 note. In order for all the notes to be played, you can try something like this:
const midiData = scribble.clip({
notes: data.map(value => notes[value - min]),
- pattern: 'x', // only play 1 note
+ pattern: 'x'.repeat(data.length), // repeat this pattern for each note in data
noteLength: '1/16',
});

Error parsing array through IF Statement

I need to loop through an entire 2D array (OldTable) to check that Column1 has a value of 1 and Col7 is not empty (null). If the above conditions are true then push the current (i) arrays of elements into newTable.
My snippet of JS is as follow...
var newTable = [];
for (var i=1; i<OldTable.length; i++){
if(OldTable[i][0]==1 && OldTable[i][7]!==null){
newTable.push(OldTable[i]);
}
}
Seems like a fairly straight forward thing to do but currently hitting brick wall with this error...
TypeError: Cannot read property "0" from undefined. (line 80, file
"Code"
I have tried to reduce the if statement to just...
if(OldTable[i][0]==1){
...but still the same error.
I'm able to display the array element just fine using...
Browser.msgBox(OldTable[50][0]);
I'm fairly new to JS so could be a simple silly error someone could point out.
UPDATE: In trying to simplying naming, I've actually made it more difficult with conflicting terminology, so have going through and updated the variable names used.
Your code should work if, as noted in the comment by #Massimo, you change your loop from starting at i=1 to i=0, as shown below. Also, just to whet your appetite for more modern tools within JavaScript, I also include an essentially identical solution to the problem using ES6/ES2015.
var myArray = [
[1, 0, 0, 0, 0, 0, 0, 'foo' ], // should pass
[9, 1, 1, 1, 1, 1, 1, 'foo' ], // should fail
[1, 2, 2, 2, 2, 2, 2, 'foo' ], // should pass
[1, 3, 3, 3, 3, 3, 3, null ], // should fail
[0, 4, 4, 4, 4, 4, 4, null ], // should fail
[1, 5, 5, 5, 5, 5, 5, undefined], // should pass
[1, 6, 6, 6, 6, 6, 6, 'foo' ] // should pass
];
function f1(array) {
var newArray = [];
for (var i = 0; i < array.length; i++) {
if (array[i][0] == 1 && array[i][7] !== null) {
newArray.push(array[i]);
}
}
return newArray;
}
const f2 = array => array.filter(e => e[0] === 1 && e[7] !== null);
console.log(f1(myArray));
console.log(f2(myArray));

Categories

Resources