简体   繁体   English

我正在尝试获取一个 javascript 函数,它将通过使用回调方法返回一个新数组,并将一个数组和两个回调作为 arg

[英]I'm trying to get a javascript function that will return a new array by using callback method and takes a array and two callbacks as arg

I want to a function to accept two callbacks and return a result only if both callbacks are true:我想要一个函数来接受两个回调并仅在两个回调都为真时才返回结果:

let myMap = function(array, cb1, cb2) {
  let result = []
  for (let i = 0; i < array.length; i++) {
    let test = cb1(array[i]) && cb2(array[i])
    result.push(test)
  }

  return result
}

The desired behavior:期望的行为:

let triple = function (n) {
    return 3 * n;
};

let half = function (n) {
    return n / 2;
};
console.log(myMap([7, 3, 2, 9, 8], triple, half));
// [ 21, 1.5, 6, 4.5, 24 ]

From the desired result, it appears that you're looking to alternate the calls of the callbacks for different array elements.从期望的结果来看,您似乎希望交替调用不同数组元素的回调。 You're not pushing/returning only if the callback is true, you're unconditionally pushing depending on the results of one of the callbacks, which is decided on by the index.您不是仅在回调为真时才推送/返回,您是无条件推送,具体取决于其中一个回调的结果,这由索引决定。

 let myMap = function(array, cb1, cb2) { let emptyArr = [] for (let i = 0; i < array.length; i++) { let test = (i % 2 === 0? cb1: cb2)(array[i]); emptyArr.push(test); } return emptyArr } let triple = function(n) { return 3 * n; }; let half = function(n) { return n / 2; }; console.log(myMap([7, 3, 2, 9, 8], triple, half));

Or, cleaned up some或者,清理一些

 const myMap = (array, cb1, cb2) => array.map( (val, i) => (i % 2 === 0? cb1: cb2)(val) ); const triple = n => 3 * n; const half = n => n / 2; console.log(myMap([7, 3, 2, 9, 8], triple, half));

暂无
暂无

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

相关问题 我正在尝试使用 ajax 将数组从 controller 传递到 javascript 但它不会以数组形式返回,而是以字符串形式返回 - I'm trying to pass an array from controller to javascript by using ajax but it doesnt return as array it returns as a String 我正在尝试使用$ .map将值放入新数组,但无法正常工作 - Using $.map i'm trying to put values into new array but is not working 具有两个等级数组的JavaScript函数 - JavaScript function that takes in two array of grades 尝试根据两个旧数组获取新数组,但出现以下错误:.push 不是 function - Trying to get a new array based on two older ones but I get the following error: .push is not a function 我正在尝试使用Angular JS中的Javascript中的循环打印数组 - I'm trying to print an array using a loop in Javascript in Angular JS 在数组的javascript find()方法中,如何仅使用回调函数的数组参数获取任何元素? 看下面的代码 - In javascript find () method of an array how to get any element using only array argument of the callback function? See the code below 我正在尝试将对象键“品牌”及其值放入一个新数组中,但我遇到了麻烦 - I’m trying to get the object key “brand” and its value into a new array but I’m having trouble 作法:以Ajax get阵列为参数的回呼函式 - How to: callback function that takes ajax get array as a parameter 设置一个回调数组并尝试在回调中使用数组索引作为值 - Setting up a array of callbacks and trying to use array index as value in callback Javascript使用.map中的Array方法回调 - Javascript Using callback in Array Method with .map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM