简体   繁体   English

需要用一个回调比较两个arrays,在Javascript中返回一个object

[英]Need to compare two arrays with a callback and return an object in Javascript

Construct a function objOfMatches that accepts two arrays and a callback.构造一个 function objOfMatches ,它接受两个 arrays 和一个回调。 objOfMatches will build an object and return it. objOfMatches将构建一个 object 并返回它。 To build the object, objOfMatches will test each element of the first array using the callback to see if the output matches the corresponding element (by index) of the second array.要构建 object, objOfMatches将使用回调测试第一个数组的每个元素,以查看 output 是否与第二个数组的相应元素(按索引)匹配。 If there is a match, the element from the first array becomes a key in an object, and the element from the second array becomes the corresponding value.如果匹配,则第一个数组中的元素成为 object 中的键,第二个数组中的元素成为相应的值。 I got this so far:到目前为止我得到了这个:

const objOfMatches = (arr1,arr2,call) => {
let result = {};
for(let i = 0; i < arr1.length; i++){
 //Don't know how to check each element using the callback
}
return result
}


const arr1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
const arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO'];
function uppercaser(str) { return str.toUpperCase(); }
console.log(objOfMatches(arr1, arr2, uppercaser)); 
// should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' }

Try this:尝试这个:

 const objOfMatches = (arr1,arr2,call) => { let result = {}; for(let i = 0; i < arr1.length; i++){ let upper = call(arr1[i]); if(arr2[i] && upper == arr2[i]) result[arr1[i]] = arr2[i]; } return result } const arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; const arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; function uppercaser(str) { return str.toUpperCase(); } console.log(objOfMatches(arr1, arr2, uppercaser));

Here is a functional programming solution:这是一个函数式编程解决方案:

 const objOfMatches = (arr1, arr2, call) => Object.fromEntries(arr1.map((v,i) => [v, arr2[i]]).filter(([a,b]) => call(a) === b)); const arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; const arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; const uppercaser = str => str.toUpperCase(); console.log(objOfMatches(arr1, arr2, uppercaser));

Explanation解释

objOfMatches is a function defined with arrow syntax, where the part that follows the (first) => is the expression that determines the function's return value. objOfMatches是用箭头语法定义的 function,其中(第一个) =>后面的部分是确定函数返回值的表达式。 The same arrow syntax is used for all other functions in this solution as well.此解决方案中的所有其他功能也使用相同的箭头语法。

First the two arrays are "zipped" into one array of pairs, using arr1.map((v,i) => [v, arr2[i]]) .首先,使用arr1.map((v,i) => [v, arr2[i]])将两个 arrays “压缩”成一对数组。 This takes every value v from arr1 and pairs it with the value from arr2 that sits at the same index.这会将arr1中的每个值varr2中位于同一索引处的值配对。 So for the example input, this produces the following:因此,对于示例输入,这将产生以下内容:

[
  ['hi', 'HI'],
  ['howdy', 'Howdy'],
  ['bye', 'BYE'],
  ['later', 'later'],
  ['hello', 'HELLO']
]

Then .filter is called on this intermediate result, only keeping the pairs [a,b] where call(a) === b .然后.filter在这个中间结果上被调用,只保留对[a,b] where call(a) === b So then we have:那么我们有:

[
  ['hi', 'HI'],
  ['bye', 'BYE'],
  ['hello', 'HELLO']
]

Finally, this is passed as argument to the Object.fromEntries function, which turns an array of key/value pairs into a plain object having those keys and values.最后,这作为参数传递给Object.fromEntries function,它将键/值对数组转换为具有这些键和值的普通 object。

 const objOfMatches = (arr1,arr2,call) => { let result = {}; arr1.forEach((item, i) => { if(call(item) === arr2[i]) { result[item] = arr2[i] } }) return result; } const arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; const arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; function uppercaser(str) { return str.toUpperCase(); } console.log(objOfMatches(arr1, arr2, uppercaser)); // should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' }

const objOfMatches = (array1, array2, callback) => array1.reduce((prev, curr) => {
  if (callback(curr) === prev.compareAgainst[0]) {
    prev.obj[curr] = prev.compareAgainst[0];
  }
  prev.compareAgainst = prev.compareAgainst.slice(1);
  return prev;
}, {obj: {}, compareAgainst: array2}).obj;

console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], (str) => str.toUpperCase()));
// should log: { hi: 'HI', bye: 'BYE', later: 'LATER' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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