user43286
user43286

Reputation: 2321

How to append elements in an array to another array?

How to append elements in an array to another array using Ramdajs with a single line statement?

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]

Upvotes: 0

Views: 660

Answers (2)

Ori Drori
Ori Drori

Reputation: 193057

Ramda's append works by "pushing" the 1st param into a clone of the 2nd param, which should be an array:

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

In your case:

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

Instead use RamdaJS's concat, and reverse the order of the parameters:

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

Upvotes: 5

Henrik R
Henrik R

Reputation: 4982

If you want to use just basic JavaScript you can do this:

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

Upvotes: 2

Related Questions