ImmutableJS - .toObject()深入

时间:2016-09-14 18:16:28

标签: immutable.js

如何将我的所有数据转换为对象?例:

//From
var a = Immutable.fromJS({ formats: ["foo", "bar"]})

a.toObject()
// How returns { formats: ["foo", "bar"] }
// How i need { formats: {0: "foo", 1: "bar"}}

由于

1 个答案:

答案 0 :(得分:1)

根据这里给出的答案:Convert Array to Object

var a = Immutable.fromJS({ formats: ["foo", "bar"]}); 
var b = {formats: toObject(a.get('formats').toJSON())}; 
console.log(b); 

function toObject(arr) {
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    if (arr[i] !== undefined) rv[i] = arr[i];
  return rv;
}

https://jsfiddle.net/o04btr3j/346/