javascript - How to add list to list in Immutable.js? - Stack Overflow

What is the best way to add List to List in Immutable.js?concat method is working, but another way is n

What is the best way to add List to List in Immutable.js?

concat method is working, but another way is not working.

const a = fromJS([  
                {
                    ment: 'aaaa',
                    who: 'a1',
                    buttonInfo: ['a', 'b', 'c'],
                },
               {
                    ment: 'bb',
                    who: 'a2',
                    buttonInfo: ['a', 'b', 'c'],
                },
            ]);

const b = fromJS([  
                {
                    ment: 'ccc',
                    who: 'c1',
                    buttonInfo: ['a', 'b'],
                },
               {
                    ment: 'ddd',
                    who: 'd2',
                    buttonInfo: ['a''],
                },
            ]);

This is working:

a.concat(b)

But this is not working:

[...a ,...b]

// or

b.map(v => {
  a.push(v);
})

What is the best way to add List to List in Immutable.js?

concat method is working, but another way is not working.

const a = fromJS([  
                {
                    ment: 'aaaa',
                    who: 'a1',
                    buttonInfo: ['a', 'b', 'c'],
                },
               {
                    ment: 'bb',
                    who: 'a2',
                    buttonInfo: ['a', 'b', 'c'],
                },
            ]);

const b = fromJS([  
                {
                    ment: 'ccc',
                    who: 'c1',
                    buttonInfo: ['a', 'b'],
                },
               {
                    ment: 'ddd',
                    who: 'd2',
                    buttonInfo: ['a''],
                },
            ]);

This is working:

a.concat(b)

But this is not working:

[...a ,...b]

// or

b.map(v => {
  a.push(v);
})
Share Improve this question edited Nov 12, 2018 at 11:38 Soroush Chehresa 5,6981 gold badge16 silver badges30 bronze badges asked Nov 12, 2018 at 10:53 kekeeekekeee 4752 gold badges7 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

you can use concat method as it said in doc:

const list1 = List([ 1, 2, 3 ]);
const list2 = List([ 4, 5, 6 ]);
const array = [ 7, 8, 9 ];
const list3 = list1.concat(list2, array);
// List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.

Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like

b.map(v => {
   a = a.push(v);
})

P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case

For add List to List in Immutable.js, you can use merge method.

Example:

const a = fromJS(
  [  
    {
      ment: 'aaaa',
      who: 'a1',
      buttonInfo: ['a', 'b', 'c'],
    },
    {
      ment: 'bb',
      who: 'a2',
      buttonInfo: ['a', 'b', 'c'],
    },
  ]
);

const b = fromJS(
  [  
    {
      ment: 'ccc',
      who: 'c1',
      buttonInfo: ['a', 'b'],
    },
    {
      ment: 'ddd',
      who: 'd2',
      buttonInfo: ['a''],
    },
  ]
);


a.merge(b);

After running tests on https://measurethat/, it seems that the concat method is more performant than spread.

By the way, the spread method should also work, but you will need to convert back to ImmutableJS:

Immutable.fromJS([...a, ...b])

Concat is the most straightforward and simple method in this case.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745495574a4630173.html

相关推荐

  • javascript - How to add list to list in Immutable.js? - Stack Overflow

    What is the best way to add List to List in Immutable.js?concat method is working, but another way is n

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信