简体   繁体   English

是否有更高阶函数从javascript中的对象数组返回一个对象?

[英]Are there any higher order function to return an object from an array of objects in javascript?

LIVE CODE EXAMPLE: 实时代码示例:

Background: 背景:

Trying to learn javascript's higher order function, some redux theory and applying it through a data transformations example and have been failing for the last few hours. 试图学习javascript的高阶函数,一些redux理论并通过数据转换实例应用它,并且在过去的几个小时里一直在失败。 :( :(

Question: 题:

How can I iterate over the approved1 or approved2 and return a new object bases on 2 cases . 如何迭代approved1approved2并返回基于2个案例的新对象。 Furthermore, is there a way to do this with a higher order function like Array.reduce() or a combination baked in higher order functions? 此外,有没有办法用更高阶函数(如Array.reduce()或高阶函数烘焙的组合Array.reduce()执行此操作? Lastly , if the final Object is wrapped in an array thats fine too. 最后 ,如果最终的Object被包装在一个很好的数组中。

I want this for a few reasons: 我想要这个有几个原因:

  • immutability. 不变性。
  • ease of testing. 易于测试。
  • learning purposes. 学习目的。

The 2 cases: 2例:

  • If all dateApproved values are !== null (in this example: approval1 array) then return a new Object (or Array wrapped Object) which looks like: 如果所有 dateApproved值都是!== null (在此示例中为: approval1数组),则返回一个新的Object (或Array wrapped Object) ,如下所示:

    {type: 'APPROVED', approvals: [...approved1]}

  • If any of the dateApproved values are equal to null (in this example: approval2 array) return a new Object (or Array wrapped Object) which looks like: 如果任何 dateApproved值等于null (在此示例中为: approval2数组),则返回一个新的Object (或Array wrapped Object) ,如下所示:

    {type: 'PENDING', approvals: [...approved2]}


JAVASCRIPT: JAVASCRIPT:

// With given logic this array evaluate with a type of 'APPROVED'
var approved1 = [

    {
        dateApproved: new Date(),
        id: 1,
    },
    {
        dateApproved: new Date(),
        id: 2,
    }
];

// With given logic this array evaluate with a type of 'PENDING'
var approved2 = [

    {
        dateApproved: null,
        id: 1,
    },
    {
        dateApproved: new Date(),
        id: 2,
    }
];

// This reducer does nothing proper right now just placeholder.
function isApproved(previousValue, currentValue, currentIdx) {
    var TYPE = ['APPROVED', 'PENDING'];
    if(previousValue.dateApproved !== null && currentValue.dateApproved !== null) {

      return currentValue
    }
}

var x = approved1.reduce(isApproved);
console.log(x); // LOG: {type: 'APPROVED' approvals: [...approved1]}

var y = approved2.reduce(isApproved);
console.log(x); // LOG: {type: 'PENDING' approvals: [...approved2]}

Use Array.prototype.every : 使用Array.prototype.every

function wrap(approvals) {
  return {
    type: approvals.every(a => a.dateApproved != null) ? 'APPROVED' : 'PENDING',
    approvals: approvals
  };
}

wrap(approved1);
// => Object {type: "APPROVED", approvals: Array[2]}

wrap(approved2);
// => Object {type: "PENDING", approvals: Array[2]}

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

相关问题 是否有任何更高阶的 function 可以遍历对象数组并返回 true 或 false - Is there any Higher order function which can iterate through a array of objects and return true or false JavaScript:调用Add函数返回新数组的高阶函数; 错误:输出数组中的'NaN'元素 - JavaScript: Higher Order Function that Calls Add Function to Return New Array; Error: 'NaN' elements in output array 访问更高阶函数内的数组对象 - access the array object inside a higher order function 从JavaScript的递归函数返回对象数组 - Return an array of objects from a recursive function in Javascript 使用高阶函数,如果另一个值为true,则返回一个对象值(JavaScript) - Using a higher order function, return one object value if another value is true (JavaScript) JavaScript:在高阶函数中使用forEach来操作/添加数组中的每个元素错误:未定义返回 - JavaScript: Use forEach within higher order function to manipulate / add to each element in array Error: undefined return 高阶 function Javascript - Higher order function Javascript JavaScript中的高阶函数 - higher order function in javascript Javascript 高阶 Function: - Javascript Higher Order Function: 使用高阶函数将嵌套数组转换为对象数组 - transform nested array into array of objects using higher order function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM