【问题标题】:Concat object array with the same name连接同名对象数组
【发布时间】:2018-01-15 04:10:51
【问题描述】:

我在 vue.js 中有一个带有 vuex 的应用程序,它实现了一些模块,我正在做的是连接对象以获取一些操作、getter、突变。 像这样:

const files = require.context('../../renderer/', true, /\?*.actions.js|.\?*.getters.js|.\?*.mutations.js$/);
const modulesSrc = {};

files.keys().forEach((key) => {
  modulesSrc[key.replace(/(\.*\/|\.js)/g, '')] = files(key).default;
});

export default modulesSrc;

我得到的是它:


当我尝试通过仅获取文件夹名称来更改正则表达式以使其工作时,它是这样的:

const files = require.context('../../renderer/', true, /\?*.actions.js|.\?*.getters.js|.\?*.mutations.js$/);
const modulesSrc = {};

files.keys().forEach((key) => {
  modulesSrc[key.replace(/(\.*\/|\.js|actions?|getters?|mutations)/g, '')] = files(key).default;
});

export default modulesSrc;

但在元素内部,它只获取最后一个文件,不与其他文件连接。


我想得到这些高亮对象(第一个正则表达式):

这个输出的内部(最后一个正则表达式):

我能做什么?

谢谢!

【问题讨论】:

    标签: javascript regex object ecmascript-6 vue.js


    【解决方案1】:

    我的建议更简单,不需要正则表达式的额外开销:

    const key = '/home/folder/something/mutations.js'
    const filename = key.substring(key.lastIndexOf('/') + 1, key.lastIndexOf('.'))
    console.log(filename)

    【讨论】:

    • 我需要文件夹的名称来为 vuex 创建一个模块,但是,使用你的代码,如果我有多个突变,我会丢弃最后一个。
    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2022-12-04
    相关资源
    最近更新 更多