最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Equivalent of Python's List Comprehension in Javascript - Stack Overflow

programmeradmin11浏览0评论

Consider the following JSON representation of an array:

logMessages = [
   {
      "timestamp":1499776056977,
      "message":"Log message c7a09226",
      "ingestionTime":1499776058327
   },
   {
      "timestamp":1499777056977,
      "message":"Log message e5d5b51a3ae1",
      "ingestionTime":1499777058327
   },
   {
      "timestamp":1499778056977,
      "message":"Log message b79f4620935b",
      "ingestionTime":1499778058327
   }
]

Suppose that I'd like to keep only timestamp and message and omit ingestionTime. In Python, I would do:

>>> [ {'timestamp': o['timestamp'], 'message': o['message']} for o in logMessages]

[{'timestamp': 1499776056977, 'message': 'Log message c7a09226'},
 {'timestamp': 1499777056977, 'message': 'Log message e5d5b51a3ae1'}, 
 {'timestamp': 1499778056977, 'message': 'Log message b79f4620935b'}]

How do I do the same field filtering in Javascript?

Consider the following JSON representation of an array:

logMessages = [
   {
      "timestamp":1499776056977,
      "message":"Log message c7a09226",
      "ingestionTime":1499776058327
   },
   {
      "timestamp":1499777056977,
      "message":"Log message e5d5b51a3ae1",
      "ingestionTime":1499777058327
   },
   {
      "timestamp":1499778056977,
      "message":"Log message b79f4620935b",
      "ingestionTime":1499778058327
   }
]

Suppose that I'd like to keep only timestamp and message and omit ingestionTime. In Python, I would do:

>>> [ {'timestamp': o['timestamp'], 'message': o['message']} for o in logMessages]

[{'timestamp': 1499776056977, 'message': 'Log message c7a09226'},
 {'timestamp': 1499777056977, 'message': 'Log message e5d5b51a3ae1'}, 
 {'timestamp': 1499778056977, 'message': 'Log message b79f4620935b'}]

How do I do the same field filtering in Javascript?

Share Improve this question asked Jul 12, 2017 at 10:29 Adam MatanAdam Matan 136k154 gold badges412 silver badges579 bronze badges 2
  • It's just a simple map function, there are plenty similar or even identical questions :( – kind user Commented Jul 12, 2017 at 10:31
  • 4 Thanks for the comment. I wanted to present the translation between Python's list comprehension and the Javascript equivalent. I think it bears value to Python developers trying to write their first JS code. – Adam Matan Commented Jul 12, 2017 at 10:40
Add a comment  | 

2 Answers 2

Reset to default 10

You can use object destructuring to obtain a subset of keys.

> logMessages= [ 
  { timestamp: 1499776056977,
    message: 'Log message c7a09226',
    ingestionTime: 1499776058327 },
  { timestamp: 1499777056977,
    message: 'Log message e5d5b51a3ae1',
    ingestionTime: 1499777058327 },
  { timestamp: 1499778056977,
    message: 'Log message b79f4620935b',
    ingestionTime: 1499778058327 } ]

> logMessages.map(({ timestamp, message }) => ({ timestamp, message }));

[ { timestamp: 1499776056977, message: 'Log message c7a09226' },
  { timestamp: 1499777056977,
    message: 'Log message e5d5b51a3ae1' },
  { timestamp: 1499778056977,
    message: 'Log message b79f4620935b' } ]

Documentation for Destructuring assignment syntax can be found:

  • Mozilla Develoepr Network

.map with an arrow function does the trick:

logMessages= [ 
  { timestamp: 1499776056977,
    message: 'Log message c7a09226',
    ingestionTime: 1499776058327 },
  { timestamp: 1499777056977,
    message: 'Log message e5d5b51a3ae1',
    ingestionTime: 1499777058327 },
  { timestamp: 1499778056977,
    message: 'Log message b79f4620935b',
    ingestionTime: 1499778058327 } ]

> logMessages.map( (o) => {return {timestamp: o.timestamp, message: o.message}})
[ { timestamp: 1499776056977, message: 'Log message c7a09226' },
  { timestamp: 1499777056977,
    message: 'Log message e5d5b51a3ae1' },
  { timestamp: 1499778056977,
    message: 'Log message b79f4620935b' } ]
发布评论

评论列表(0)

  1. 暂无评论