我想改变这个:
[ 'para', 'this is a paragraph']
进入此(选择数组第二个字符串的第一个树单词,然后将它们放入带有附加 em
的嵌套数组中)。
[ 'para', [ 'em', 'this is a' ], 'paragraph' ]
所以我尝试了以下方法:
var para = tree[1]
var firstTreeItems = para[1].split(' ', 3).join(' ')
para[1] = _.without(para[1].split(' '), firstTreeItems.split(' ')).join(' ')
para.splice(1, 0, ['em', res])
console.log(para)
但它不起作用,_.without
并没有排除数组最后部分的this is a
:
[ 'para', [ 'em', 'this is a' ], 'this is a paragraph' ]
我做错了什么?
最佳答案
将两行更改为:
var firstTreeItems = para[1].split(' ', 3)
para[1] = _.without(para[1].split(' '), firstTreeItems[0], firstTreeItems[1], firstTreeItems[2]).join(' ')
_.without
并不是要删除您提供的 firstTreeItems
数组的元素,而是要删除数组本身,但没有找到它。
首先,我将 firstTreeItems
保留为数组,以便我可以单独访问每个元素。然后,我将每个不需要的元素赋予 _.without
,以从临时 para[1].split(' ')
数组中删除。
关于javascript - 下划线的_.without不排除下面数组中的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061724/