罗达什;替换并合并两个字符串数组

lodash; replace and merge two arrays of strings

lodash 中是否有合并两个数组的函数,例如;

var arr_1 = ['1', '0', '1'], arr_2 = ['0', '1', '1'];

结果应该return:

console.log(new_arr); // ['0', '0', '1']

更新

每个数组有 3 个元素(字符串)。每当在两个数组中的任何一个中找到 '0' 时,它都必须保留在结果中。

这个怎么样:

ES5:

_.map(_.zip(arr_1,arr_2), function(values) { 
      return values[0] <= values[1] ? values[0] : values[1]; 
})

ES6:(更好)

_.zip(arr_1,arr_2).map( v => v[0] <= v[1] ? v[0] : v[1] ); 

不只是一种方法,它使用了lodash zip

以下应该可以满足您的需求。它使用 flatten() to create one array, and from there, reduce() 创建一个新数组,允许“0”个重复项,并删除所有其他重复项。

_([arr_1, arr_2])
    .flatten()
    .reduce(function(result, item) {
        if (item === '0' || !_.includes(result, item)) { 
            result.push(item);
        }
        return result; 
    }, []);