下划线的_.without不排除后面数组中的子串?
Underscore's _.without not excluding substring in the following array?
我想转这个:
[ '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(' ')
数组中删除。
我想转这个:
[ '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(' ')
数组中删除。