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

How to append elements in an array to another array?

如何通过单行语句使用 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]

Ramda 的 append 通过 "pushing" 将第一个参数复制到第二个参数的克隆中,它应该是一个数组:

R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]

你的情况:

R.append([1,2,3,4], [10,11]); // => [10,11,[1,2,3,4]]

改用RamdaJS的concat,把参数的顺序倒过来:

R.concat(state.taggable_friends, action.payload)

如果你只想使用基本的 JavaScript 你可以这样做:

return {
  ...state,
  taggable_friends: [...state.taggable_friends, action.payload],
}