我有一个类似这样的功能:
var exchange = function(code) {
var ref = {
'one' : '1' ,
'two' : '2' ,
'three' : '3'
};
return code.split(' ').map( function (a) {
return a.split(' ').map( function (b) {
return ref[b];
}).join('');
}).join(' ');
};
Run Code Online (Sandbox Code Playgroud)
所以现在我做到了:
var strg = "one two three";
alert( exchange( strg ) ); // => 123
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我有问题。让我解释。我希望它执行与现在相同的操作,直到之间没有空格。
例如 :
var strg = "onetwothree";
alert( exchange( strg ) ); // => Nothing
Run Code Online (Sandbox Code Playgroud)
但是我希望它即使没有空格也可以更改文本。我怎样才能做到这一点 ?
让我们考虑一个数组:
var a = ["one", "two", "three"];
Run Code Online (Sandbox Code Playgroud)
现在,要更新数组,我必须执行以下操作:
a[0] = "1";
a[1] = "2";
a[2] = "3";
Run Code Online (Sandbox Code Playgroud)
但如果数组更大,我就无法重复此操作。我想要一个函数,借助它,我可以这样做:
a.update(0, "1", 2, "3", 3, "4"); // => ["1", "two", "3", "4"]
Run Code Online (Sandbox Code Playgroud)
是的,您看到在这个帮助下我添加了第四个属性,而第一个和第三个属性得到了更新?那么,这个可以制作吗?或者有更好的方法来执行上述任务?
提前致谢
我一直在从事 Svelte 3 + Electron 12.0.5 项目。我正在使用svelte-spa-router包进行哈希路由。我的项目结构如下所示:
node_modules/
public/
build/
bundle.css
bundle.js
index.html
src/
Components/
Pages/
Home.svelte
Settings.svelte
...
Sidebar.svelte
Titlebar.svelte
App.js
App.svelte
...
index.js // <-- Electron entry point
rollup|svelte|tailwind // <-- config files
Run Code Online (Sandbox Code Playgroud)
由于我使用的是路由器,所以电子的window.loadFile()
无法工作;为了解决这个问题,我同时使用了Sirv -cli和. 现在我的启动脚本如下所示:
"start": "concurrently \"sirv public --no-clear --port=40072\" \"electron .\""
Run Code Online (Sandbox Code Playgroud)
现在我曾经window.loadURL("https://localhost:40072")
让这个工作。.svelte
文件,在<script>
标签内,我尝试这样做import * as electron from "electron"
;但这导致了一个错误,说fs
未定义。所以现在,我在内部创建了一个快速服务器index.js
,并使用它向服务器fetch
发出POST
请求并执行我可以轻松完成的操作ipcMain
,并且ipcRenderer
...我不知道我是否做错了什么(nodeIntegration 是已设置为 true)。我对 …
CSS具有@ media,@ keyframes等规则。可以使用javascript(例如@myCustomRule)来实现。如果是,那怎么办?如果没有,那么有没有其他选择或仅使用CSS?
我有一个可以从字符串中删除单词的函数。这是 :
var removeFromString = function(oldStr, fullStr) {
return fullStr.split(oldStr).join('');
};
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
console.log( removeFromString("Hello", "Hello World") ); // World
console.log( removeFromString("Hello", "Hello-World") ); // -World
Run Code Online (Sandbox Code Playgroud)
但主要问题是:
var str = "Remove one, two, not three and four";
Run Code Online (Sandbox Code Playgroud)
这里我们必须删除“一”、“二”和“四”。这可以通过以下方式完成:
var a = removeFromString("one, two,", str); // Remove not three and four
var b = removeFromString("and four", a); // Remove not three
console.log(b); // Remove not three
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我不得不使用该函数两次。我希望它是这样的:
var c = removeFromString(["one, two," , "and four"], str); // Remove not three
console.log(c); // …
Run Code Online (Sandbox Code Playgroud) 我在 ES6 标准中有以下代码:
function test(html, ...args) {
return html.slice(1).reduce((str, elem, i) => str + args[i] +
elem, html[0]);
}
Run Code Online (Sandbox Code Playgroud)
这是使用它:
var hello = 'Hello';
var world = 'Arcanadian Arc';
document.body.innerHTML = test`<h1>${hello} ${world}!</h1>`;
Run Code Online (Sandbox Code Playgroud)
输出 :
<h1>Hello Arcanadian Arc!</h1>
Run Code Online (Sandbox Code Playgroud)
由于它是 ES6 标准代码,IE 不支持它。所以,我决定使用 Babel 将其转译为 ES5,这就是我所做的。但我对输出并不满意。这是输出:
function _templateObject() {
var data = _taggedTemplateLiteral(["<h1>", " I am very happy </h1>"]);
_templateObject = function _templateObject() {
return data;
};
return data;
}
function _taggedTemplateLiteral(strings, raw) { if (!raw) {
raw = strings.slice(0); } …
Run Code Online (Sandbox Code Playgroud) 假设我们有一个名为data-content的自定义属性。现在让我们寻找html标记:
<h1 data-content="one two three four">Hello</h1>
Run Code Online (Sandbox Code Playgroud)
我想要一个可以执行以下操作的Javascript / jQuery函数:
verify.contains("h1", "two"); // => true
verify.contains("h1", "five"); // => false
Run Code Online (Sandbox Code Playgroud)
要么
$("h1").verifyContains("two"); // => true
$("h1").verifyContains("five"); // => false
Run Code Online (Sandbox Code Playgroud)
那么,该怎么做呢?
让我们考虑一个变量str定义为:
var str = "A, B, C, D, E, F"
Run Code Online (Sandbox Code Playgroud)
要在逗号处拆分字符串,我会这样做:
str = str.split(",")
console.log(str)
// => ["A", "B", "C", "D", "E", "F"]
Run Code Online (Sandbox Code Playgroud)
那么,如果我必须在前两个逗号处拆分它,我该怎么办,以便输出变为:
// => ["A", "B", "C, D, E, F"]
Run Code Online (Sandbox Code Playgroud)