gpt4 book ai didi

javascript - 如何创建以下数组的克隆而不是引用?

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:29 24 4
gpt4 key购买 nike

<分区>

代码(一个简单的替换循环):

fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err

data = data.split('\n\n')
var tree = data

for (var i = 0; i < tree.length; ++i) {
if (tree[i].match(/^#/g)) {
data[i] = data[i]
.replace(/^#### (.*)/gm, '<h4>$1</h4>')
.replace(/^### (.*)/gm, '<h3>$1</h3>')
.replace(/^## (.*)/gm, '<h2>$1</h2>')
.replace(/^# (.*)/gm, '<h1>$1</h1>')
}
}

data = data.join('\n\n')
tree = tree.join('\n\n')

console.log(data)
console.log(tree)

所以在这种情况下,datatree 都有相同的输出:

<h1>Test</h1>

<h2>Test</h2>

Lorem "**ipsum?**" dolor 'sit!' amet

Anooo

* * *

"Ipsum?" "'**dolor**'" *sit!* amet, consetetur
eirmod tempor--invidunt **ut** labore

Anothes

我想做的是在tree中保留data的原始值,这样只有data改变,tree不改变

怎么做?

tree 应该保持这样:

# Test

## Test

Lorem "**ipsum?**" dolor 'sit!' amet

Anooo

* * *

"Ipsum?" "'**dolor**'" *sit!* amet, consetetur
eirmod tempor--invidunt **ut** labore

Anothes

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com