简体   繁体   中英

input array as a param?

this is my code:

 return s => counts.get(s) || 0; // Sample call

I want this function to input 2 array's and on output an single array for example I call a function.

Example:

f(a,b)=array result with a and b two array

You could reduce the counts and map the count of given values. Then you need to udjust undefined values.

 function fn(values, counts) { return values .map( Map.prototype.get, counts.reduce((map, s) => map.set(s, (map.get(s) || 0) + 1), new Map) ) .map(v => v || 0); } console.log(fn(["wer", "tyu", "uio"], ["wer", "wer", "tyu", "oio", "tyu"]));

This is how you combine two arrays into one, Using the built in Array.prototype.concat

Solution

 function combineArrays (a, b) { return a.concat(b); } const array1 = [1,2,3]; const array2 = [4,5,6]; console.log(combineArrays(array1, array2));

However, your code example doesn't seem to match what you're asking. Perhaps you want to combine all counts from both arrays into a single value?

 function addArrays(a, b) { return a.concat(b).reduce((a, b) => a + b); } const array1 = [1,2,3]; const array2 = [4,5,6]; console.log(addArrays(array1, array2));

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM