我知道这[].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)
任何人都可以给我一个模糊的想法吗?
我正在使用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)
感觉它应该有效,至少从我在文档中读到的内容来看 - 但我对续集没有完全的经验或信心,所以任何帮助将不胜感激。
谢谢
我可以查找一个数组是否存在于另一个数组中:
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) 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 …
我敢肯定,这是一个简单的问题,但是我看不到为什么这样的代码:
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">
我犯了一个简单的错误,还是我不理解的行为?谢谢
javascript ×4
arrays ×2
express ×1
html ×1
jestjs ×1
mysql ×1
node.js ×1
sequelize.js ×1
webpack ×1