如何将数组中的元素追加到另一个数组?

use*_*286 0 immutability ramda.js

如何使用带有单行语句的Ramdajs将数组中的元素追加到另一个数组?

state = {
   items:[10,11,]
 };

newItems = [1,2,3,4];

state = {
  ...state,
  taggable_friends: R.append(action.payload, state.taggable_friends)
}; 

//now state is [10,11,[1,2,3,4]], but I want [10,11,1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

Ori*_*ori 5

Ramda的append作品是将第一个参数"推"到第二个参数的克隆中,该副本应该是一个数组:

R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

R.append([1,2,3,4], [10,11]); // => [10,11,[1,2,3,4]]
Run Code Online (Sandbox Code Playgroud)

而是使用RamdaJS concat,并反转参数的顺序:

R.concat(state.taggable_friends, action.payload)
Run Code Online (Sandbox Code Playgroud)