bokzor
bokzor

Reputation: 445

Add an Item in OrderedMap with Immutable.js

How can I add an Item at the end of an OrderedMap ?

I tried

    this.boxes = this.boxes.setIn(data);

Thanks for any help

Upvotes: 2

Views: 4075

Answers (2)

Smilev
Smilev

Reputation: 429

One possible way of doing it is using concat

var boxes = Immutable.OrderedMap({
  box1: {
    id:1
  },
  box2: {
    id:2
  }
});

var data = Immutable.fromJS({
  box3: {
    id:3
  }
});


var newBoxes = boxes.concat(data);
console.log(newBoxes.toJS());  

Will print out:

Object { box1: Object { id: 1 }, box2: Object { id: 2 }, box3: Object { id: 3 } }

Upvotes: 4

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

An OrderedMap doesn't really have an "end" per se. Also if you want to add an item to a Map you're going to need a key for it as well. So either you use the set method for adding a key-value pair:

this.boxes = this.boxes.set(key,data);

Or you convert it to a List, where you can add it to the end and also maintain a clear iteration order, meaning you do get to have an "end".

this.boxes = this.boxes.toList().push(data);

Upvotes: 3

Related Questions