小编Nic*_*ckW的帖子

展平一个数组?[].concat(...[数组])?

我知道这[].concat(...array)会展平一组数组,但我一直在学习 webpack 课程,并且在加载预设的代码中它使用了语法[].concat(...[array])

我对它的理解是:

const array = [[1,2,3], [4,5,6], [7,8,9]];
const result = [].concat(...array);       // [1,2,3,4,5,6,7,8,9]
const result2 = [].concat(...[array]);    // [[1,2,3], [4,5,6], [7,8,9]]
Run Code Online (Sandbox Code Playgroud)

这绝对让我感到困惑,因为课程代码(下面)确实有效,但我看不到[].concat(...[array])实现了什么?

const webpackMerge = require("webpack-merge");

const applyPresets = (env = {presets: []}) => {
  const presets = env.presets || [];
  /** @type {string[]} */
  const mergedPresets = [].concat(...[presets]);
  const mergedConfigs = mergedPresets.map(presetName =>
    require(`./presets/webpack.${presetName}`)(env)
  );

  return webpackMerge({}, ...mergedConfigs);
};

module.exports = applyPresets;
Run Code Online (Sandbox Code Playgroud)

任何人都可以给我一个模糊的想法吗?

javascript arrays webpack

5
推荐指数
1
解决办法
82
查看次数

Sequelize:从多列中选择不同的字段值

我正在使用sequelize 和mysql2,需要从多个列中选择所有不同的值。例如,我有一个“标题”和一个“位置”列。

目前这个:

Job.findAll({
    attributes: [
        [Sequelize.fn('DISTINCT', Sequelize.col('title')), 'title']
    ]
})
Run Code Online (Sandbox Code Playgroud)

有效,返回所有不同的职位名称。但是,如果我为位置列添加第二行,则会出现语法错误

Job.findAll({
    attributes: [
        [Sequelize.fn('DISTINCT', Sequelize.col('title')), 'title'],
        [Sequelize.fn('DISTINCT', Sequelize.col('location')), 'location']
    ]
})
Run Code Online (Sandbox Code Playgroud)

感觉它应该有效,至少从我在文档中读到的内容来看 - 但我对续集没有完全的经验或信心,所以任何帮助将不胜感激。

谢谢

javascript mysql sequelize.js

5
推荐指数
1
解决办法
1万
查看次数

使用高阶函数查找数组中数组的索引?

我可以查找一个数组是否存在于另一个数组中:

const arr1 = [[1,2,3],[2,2,2],[3,2,1]];

const match = [2,2,2];

// Does match exist
const exists = arr1.some(item => {
  return item.every((num, index) => {
    return match[index] === num;
  });
});
Run Code Online (Sandbox Code Playgroud)

我可以找到该数组的索引:

let index;
// Index of match
for(let x = 0; x < arr1.length; x++) {
  let result;
  for(let y = 0; y < arr1[x].length; y++) {
    if(arr1[x][y] === match[y]) { 
      result = true; 
    } else { 
      result = false; 
      break; 
    }
  }
  
  if(result === true) { 
    index = x; …
Run Code Online (Sandbox Code Playgroud)

javascript arrays higher-order-functions

4
推荐指数
1
解决办法
399
查看次数

Server instances, jest and 'listen EADDRINUSE :::3000'

Am new to jest, node and express, and am having a problem with testing my app.

The actual code seems to be working - it's just when passing the server instance to each of the test files (user.test.js and genres.test.js) and running jest, the port is being blocked. I assume it's because I'm creating duplicate instances of the server in each test file, without realising it.

Running jest with the flag --runInBand works, and so does only using one test …

node.js express jestjs

1
推荐指数
1
解决办法
539
查看次数

使用模板文字添加多个类名

我敢肯定,这是一个简单的问题,但是我看不到为什么这样的代码:

const addDiv = (test) => {
    const markup = `
      <div class=${test? "name":"name name-active"}></div>
    `;
    element.insertAdjacentHTML('beforeend', markup);
};
Run Code Online (Sandbox Code Playgroud)

结果<div class="name" name-active="">而不是<div class="name name-active">

我犯了一个简单的错误,还是我不理解的行为?谢谢

html javascript

0
推荐指数
1
解决办法
52
查看次数