【问题标题】:Get specific key and value from multiple objects从多个对象中获取特定的键和值
【发布时间】:2017-01-31 10:21:19
【问题描述】:

状态:

// WebdriverIO log function

browser.log('browser').then(function(logs) {
    console.log(logs.value);
});

返回以下数组:

[ { level: 'INFO',
    message: 'https://localhost:3000/ 42:14 "foo"',
    timestamp: 1485857149752 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 43:14 "bar"',
    timestamp: 1485857149752 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 46:14 Array[6]',
    timestamp: 1485857149755 },
  { level: 'SEVERE',
    message: 'https://localhost:3000/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
    timestamp: 1485857149834 } ]

目标:

我想创建一个稍后可以使用的方法,它会返回一个日志条目列表。在我的示例应用程序中就是这种情况:

foo
bar

我不知道如何仅循环获取“消息”键及其值。

要过滤以便只获得“foo”或“bar”,我会使用 RegEx 或 .split 函数。

【问题讨论】:

  • 您想过滤以获取仅包含 messages 的列表吗?
  • Failed to load resource: the server responded with a status of 404 (Not Found) - 最后一条消息的值?
  • 如何知道从"foo""bar" 中删除引号并将Array[6] 更改为[Array]
  • @kukkuz 是的,我需要它来将其用作进一步测试的辅助函数。所以场景是我用 mocha/webdriverio 测试了一个应用程序,并想检查控制台日志是否已建立对等连接等。

标签: javascript arrays unit-testing object webdriver-io


【解决方案1】:

您可以使用Array.prototype.reduce()

let arr = [{
  level: 'INFO',
  message: 'https://localhost:3000/ 42:14 "foo"',
  timestamp: 1485857149752
}, {
  level: 'INFO',
  message: 'https://localhost:3000/ 43:14 "bar"',
  timestamp: 1485857149752
}, {
  level: 'INFO',
  message: 'https://localhost:3000/ 46:14 Array[6]',
  timestamp: 1485857149755
}, {
  level: 'SEVERE',
  message: 'https://localhost:3000/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
  timestamp: 1485857149834
}];

let res = arr.reduce((a, b) => {
  if (b.level === 'INFO') {
    a.push(b.message.split(" ").pop().replace(/"/g, ""));
  }
  return a;
}, []);

console.log(res);

记得在链接所有的之前进行检查

b.message.split(" ").pop().replace(/"/g, "")

避免出现空指针异常。

旁注:

OP 没有指定,但是如果您要收集的消息也包含空格,您可以替换上面的拆分并使用@JLRishe's regex instead

【讨论】:

  • 如果任何消息包含空格,这将无法正常工作。
  • @JLRishe 感谢您的提醒,我添加了一个涵盖该案例的版本。顺便提一句。你的也不适用于同样的情况。
  • 漂亮的正则表达式@JLRishe
【解决方案2】:

您可以执行以下操作:

var log = [ { level: 'INFO',
    message: 'https://localhost:3000/ 42:14 "foo"',
    timestamp: 1485857149752 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 43:14 "bar"',
    timestamp: 1485857149752 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 46:14 Array[6]',
    timestamp: 1485857149755 },
  { level: 'SEVERE',
    message: 'https://localhost:3000/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
    timestamp: 1485857149834 } ]

var messages = log
    .filter(function (entry) { return entry.level === 'INFO'; })
    .map(function (entry) { 
        return entry.message.replace(/^([^\s]+\s){2}"?|"$/g, ''); 
    });

console.log(messages)

这不会将"foo" 转换为foo 等等,但不清楚您希望代码如何做到这一点(我已在您的问题中添加了一条评论)。

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 2022-01-21
    • 2022-01-02
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多