如何从数组中提取某些单词?

时间:2019-07-06 21:33:11

标签: javascript arrays

我有这个数组供我使用:

const databaseEntry = ['Faction: First Galactic Empire', 'Name: Firms Piett', 'Rank: Admiral'];

我的任务是从此数组中创建一个变量,其结果如下所示

'Firms Piett, Admiral of the First Galactic Empire.'

我已经尝试过并重试过,但是我还是数组新手

4 个答案:

答案 0 :(得分:1)

将数据重新组织为对象,以便具有更简单的键值关联,然后构建句子:

const foo = entry => {
  const o = {};
  for(let e of entry) {
    let [key, value] = e.split(': ');
    o[key]=value;
  }
  return `${o.Name}, ${o.Rank} of the ${o.Faction}`;
}

console.log(foo(['Faction: First Galactic Empire', 'Name: Firms Piett', 'Rank: Admiral']));

由于您是初学者,因此可能需要查看String.splitarray destructuring才能完全理解上面的代码。

答案 1 :(得分:0)

使用String.split()

遍历数组中的每个项目,并在空间上拆分。这将为您为每个项目提供两个值的数组。例如,["Faction:", "First Galactic Empire"]。然后,您可以使用索引值获取字符串。

答案 2 :(得分:0)

欢迎使用StackOverflow:)

INFO: start ffmpeg: [C:\Program Files\ffmpeg\bin\ffmpeg.exe, -y, -i, C:\Users\USERNAME\Desktop\Test_Workflow\1.mkv, -ss, 0.0, -i, C:\Users\USERNAME\Desktop\Test\resources/videos/P1000501.MP4\audio.ogg, -c, copy, -map, 0:v, -map, 1:a, -bsf:v, h264_mp4toannexb, -r, 25, -shortest, C:\Users\USERNAME\Desktop\Test\testoutput.mp4]
ffmpeg version N-92087-gdcbd89e000 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20180813
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 19.101 / 56. 19.101
  libavcodec     58. 31.102 / 58. 31.102
  libavformat    58. 18.104 / 58. 18.104
  libavdevice    58.  4.105 / 58.  4.105
  libavfilter     7. 33.100 /  7. 33.100
  libswscale      5.  2.100 /  5.  2.100
  libswresample   3.  2.100 /  3.  2.100
  libpostproc    55.  2.100 / 55.  2.100
Input #0, matroska,webm, from 'C:\Users\USERNAME\Desktop\Test_Workflow\1.mkv':
  Metadata:
    ENCODER         : Lavf56.4.101
  Duration: 00:00:04.60, start: 0.080000, bitrate: 1377 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Input #1, ogg, from 'C:\Users\USERNAME\Desktop\Test\resources/videos/P1000501.MP4\audio.ogg':
  Duration: 00:04:20.65, start: 0.000000, bitrate: 86 kb/s
    Stream #1:0(und): Audio: vorbis, 48000 Hz, stereo, fltp, 112 kb/s
    Metadata:
      CREATION_TIME   : 2018-11-09T13:20:07.000000Z
      LANGUAGE        : und
      ENCODER         : Lavc58.31.102 libvorbis
      MAJOR_BRAND     : mp42
      MINOR_VERSION   : 1
      COMPATIBLE_BRANDS: mp42avc1
[mp4 @ 000001e67c0a00c0] track 1: codec frame size is not set
Output #0, mp4, to 'C:\Users\USERNAME\Desktop\Test\testoutput.mp4':
  Metadata:
    encoder         : Lavf58.18.104
    Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Stream #0:1(und): Audio: vorbis (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 112 kb/s
    Metadata:
      CREATION_TIME   : 2018-11-09T13:20:07.000000Z
      LANGUAGE        : und
      ENCODER         : Lavc58.31.102 libvorbis
      MAJOR_BRAND     : mp42
      MINOR_VERSION   : 1
      COMPATIBLE_BRANDS: mp42avc1
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  113 fps=0.0 q=-1.0 Lsize=     824kB time=00:00:04.41 bitrate=1530.8kbits/s speed= 634x    
video:772kB audio:43kB subtitle:0kB other streams:0kB global headers:4kB muxing overhead: 1.092082%

这是解决您问题的简便方法。而且,由于您似乎不熟悉JavaScript,因此这可能是帮助您理解数组的最佳方法:)

答案 3 :(得分:0)

使用mapsplit和数组解构的非常简单的单行代码。

const databaseEntry = ['Faction: First Galactic Empire', 'Name: Firms Piett', 'Rank: Admiral'];

const [faction, name, rank] = databaseEntry.map(e => e.split(": ")[1]);

console.log(`${name}, ${rank} of the ${faction}`);