用ES语法替换循环

时间:2018-10-01 22:02:12

标签: javascript

我正在尝试使用ES语法重写我的循环。我试图只替换一个循环,但是它被调用了几次。如何使用ES语法返回单个值?输出应为未定义或单个值。

输入示例:

[{"events": [{"type": "Change", "field_name": "123"}], "created_at": "2017-07-01"}, 
{"events": [{"type": "Change", "field_name": "456"}], "created_at": "2017-08-01"},
{"events": [{"type": "Change", "field_name": "789"}], "created_at": "2017-09-01"}]

函数:

function extractAuditEventDate(data, field, type) {
    console.log(data)
    for (var i = 0; i < data.length; i++) {
        const audit = data[i]

        // const event = audits.events.find(e => e.type === type && e.field_name === field)

        for (var j = 0; j < audit.events.length; j++) {
            const event = audit.events[j]

            if (event.type === type && event.field_name === field) {
                //console.log(audits.created_at)
                return audit.created_at
            }
        }
    }
}

如果我extractAuditEventDate并将参数数据作为输入示例传递,将'456'作为字段名称并将'Change'作为类型传递,我期望得到'2018-07-01'

3 个答案:

答案 0 :(得分:1)

你去了!

const data = [{"events": [{"type": "Change", "field_name": "123"}], "created_at": "2017-07-01"}, 
{"events": [{"type": "Change", "field_name": "456"}], "created_at": "2017-08-01"},
{"events": [{"type": "Change", "field_name": "789"}], "created_at": "2017-09-01"}]

// returns false if can't find the result
const extractAuditEventDate = (data, field, type) => {
    let hit;
    try {
      hit = data.find((el) => el.events.find(
       (e) => (e.type === type && e.field_name === field)))['created_at'];   
    } catch(err) {
      hit = false;
    }
    return hit;
}

console.log(
  extractAuditEventDate(data, '456', 'Change'), // Found it
  extractAuditEventDate(data, '789536', 'Change'), // Not Found (false)
  extractAuditEventDate(data, '456', 'IWILLFAIL'), // Not Found (false)
  extractAuditEventDate(data, {}) // What are you doing (false)
);

答案 1 :(得分:1)

用于处理未找到商品的情况的小选择:

const data = [{"events": [{"type": "Change", "field_name": "123"}], "created_at": "2017-07-01"}, 
              {"events": [{"type": "Change", "field_name": "456"}], "created_at": "2017-08-01"},
              {"events": [{"type": "Change", "field_name": "789"}], "created_at": "2017-09-01"}]

const extractAuditEventDate = (data, field, type) => ({...
  data.find(audit => audit.events.some(event => 
    event.type === type && event.field_name === field))}).created_at   

console.log( extractAuditEventDate(data, '123', 'Change') )
console.log( extractAuditEventDate(data, '123', 'change') )

由于开销大,ES6功能替代品应该比ES版本慢几倍。


更快更易读的替代方法可以是for...of循环(IE中不支持):

const data = [{"events": [{"type": "Change", "field_name": "123"}], "created_at": "2017-07-01"}, 
              {"events": [{"type": "Change", "field_name": "456"}], "created_at": "2017-08-01"},
              {"events": [{"type": "Change", "field_name": "789"}], "created_at": "2017-09-01"}]

function extractAuditEventDate(data, field, type) {
  for (let audit of data)
    for (let event of audit.events)
      if (event.type === type && event.field_name === field)
        return audit.created_at
}

console.log( extractAuditEventDate(data, '123', 'Change') )
console.log( extractAuditEventDate(data, '123', 'change') )

答案 2 :(得分:-2)

在开发控制台上尝试:

var j =  [{"events": [{"type": "Change", "field_name": "123"}], "created_at": "2018-07-01"}, 
{"events": [{"type": "Change", "field_name": "456"}], "created_at": "2017-08-01"},
{"events": [{"type": "Change", "field_name": "789"}], "created_at": "2017-09-01"}];

var date = '2018-07-01';
var fieldName = '123';
var type = 'Change';

j.find(c => c.created_at == date).events.find(event => event.type == type && event.field_name == fieldName)