2017-05-25 62 views
0

我收到大阵对:[number, {number,number, big array of numbers}]在ramda.js中,fromPairs是否改变了元素的顺序?

首先我我主要对添加到数组的开头:

prepend([target[0], {count : target[1].length, overall : target[1].length, items:target[1]}]), 

接下来我做的:

Promise.all([           
      to_file_json(
      token.user_id, 
      'connections_data', 
      JSON.stringify(
      fromPairs(r[0]) 
     ))... 

而且我能找到我的主在我的文件中间的某个地方配对。

所以我的问题是,fromPairs可能会改变顺序?如果是,我能做些什么来防止这种情况发生?

编辑:

附加信息:

1)r变量对应于[[number, Friends][], Float64Array[]]

2)target变量对应于[number,number[]]

3)元件的初期其中我是前置,它总是最大的一个,它以某种方式处于文件的中间。

"136444868":{"count":304,"overall":304,"items":[19363,234566,290677,1375661,2030175,2131497,2593602,2596894,2816890,2869895,3170377,3437884,3486703,3504543,4046799,4235623,5366101..... 

4)友类型:

interface Friends { 
    count:number, 
    overall:number, 
    items:number[] 
    }; 

样本数据

{ 
    "19363":{"count":5,"overall":3088,"items":[51177198,53119509,136035431,209482119,216378147]} 

    ,"234566":{"count":6,"overall":6803,"items":[290677,3504543,23180680,75311610,178479726,196401211]} 

    ,"290677":{"count":19,"overall":2213,"items":[234566,5686439,7873089,11175816,13726459,20697213,23180680,27419631,55209039,74493674,75311610,125041200,133272552,139307068,159591583,168386810,173599247,178429642,189097165]} 


    ,"1375661":{"count":0,"overall":76,"items":[]},"2030175":{"count":14,"overall":86,"items":[2596894,6507568,11681736,17736119,49557638,117771194,127144880,141523415,147264238,153044182,156925389,160656334,223530741,262311445]},"2131497":{"count":16,"overall":301,"items":[13598979,15682478,20357560,20869716,27419631,30869837,33650605,40129023,68976427,88146695,90648231,101105191,118193129,145163503,216503667,387266562]}, 
+0

你能展示一些演示这个问题的示例数据吗? –

+0

好的一刻 –

+0

请注意,拉姆达认为对象是无序集合。但是,假设底层引擎不重新排序,该对象可能会有按照它们添加的顺序迭代的键。这意味着'fromPairs'和'toPairs'应该像反向一样。不过,拉姆达本身并不强制执行此操作。 –

回答

3

我想到的问题是,你做一个prepend未经其在列表中后位置删除元素。

那么你最终可能会与像一些数据:

[ 
    [ 2131497, { count: 16, overall: 301, items: [ /* .. * ] } ], // duplicate 
    [ 19363, { count: 5, overall: 3088, items: [ /* .. * ] } ], 
    [ 234566, { count: 6, overall: 6803, items: [ /* .. * ] } ], 
    [ 290677, { count: 19, overall: 2213, items: [ /* .. * ] } ], 
    [ 1375661, { count: 0, overall: 76, items: [ /* .. * ] } ], 
    [ 2030175, { count: 14, overall: 86, items: [ /* .. * ] } ], 
    [ 2131497, { count: 16, overall: 301, items: [ /* .. * ] } ] // duplicate 
] 

然后,当你做fromPairs,以后的版本将覆盖较早的企业之一,它最终将返回该列表中的原始位置,按照文档中的这一行:

如果一个键出现在多对中,则最右边的对包含在该对象中。

但是......即使你解决这个问题,你仍然不会得到你想要的行为,因为对象属性iteration order specification,该说的对象是整数键首先迭代,按数字顺序的,前非整数键。阿克塞尔Rauschmayer有这个非常readable description

这些复杂性是Ramda(免责声明:我是其中一位作者)未创建foldObj实现的原因之一。

+0

谢谢你解释! –