2017-01-18 40 views
0

我有一塊JSON數據,我使用動態生成的表單,它看起來像這樣:將兩段不匹配的數據與immutable.js合併?

{ 
    "form": { 
    "fields": [ 
     { 
     "name": "First Name", 
     "alt": "first_name" 
     }, 
     { 
     "name": "Last Name", 
     "alt": "last_name" 
     }, 
     { 
     "name": "D.O.B.", 
     "alt": "dob" 
     }, 
     { 
     "name": "Primary Address", 
     "alt": "primary_address" 
     }, 
     { 
     "name": "Primary City", 
     "alt": "primary_city" 
     }, 
     { 
     "name": "ZIP Code", 
     "alt": "zip" 
     }, 
     { 
     "name": "Country", 
     "alt": "country" 
     }, 
     { 
     "name": "First 4 Phone Digits", 
     "alt": "first_phone" 
     }, 
     { 
     "name": "Phone Number", 
     "alt": "phone_number" 
     } 
    ] 
    }, 
    "someVal": [], 
    "someVal2": "a string" 
} 

當我提交此表格,並有錯誤,我返回的錯誤的格​​式如下:

[ 
     { 
     "alt": "primary_city", 
     "errors": [ "primary_city Error Message" ] 
     }, 
     { 
     "alt": "zip", 
     "errors": [ "zip Error Message" ] 
     }, 
     { 
     "alt": "first_name", 
     "errors": [ "first_name Error Message" ] 
     }, 
     { 
     "alt": "dob", 
     "errors": [ "dob Error Message" ] 
     }, 
     { 
     "alt": "primary_address", 
     "errors": [ "primary_address Error Message" ] 
     }, 
     { 
     "alt": "last_name", 
     "errors": [ "last_name Error Message" ] 
     } 
    ] 

我的目標是合併兩個獲得這個格式,合併的錯誤,其中ALTS比賽:

{ 
     "form": { 
     "fields": [ 
      { 
      "name": "First Name", 
      "alt": "first_name", 
      "errors": [ "first_name Error Message" ] 
      }, 
      { 
      "name": "Last Name", 
      "alt": "last_name", 
      "errors": [ "last_name Error Message" ] 
      }, 
      { 
      "name": "D.O.B.", 
      "alt": "dob", 
      "errors": [ "dob Error Message" ] 
      }, 
      { 
      "name": "Primary Address", 
      "alt": "primary_address", 
      "errors": [ "primary_address Error Message" ] 
      }, 
      { 
      "name": "Primary City", 
      "alt": "primary_city", 
      "errors": [ "primary_city Error Message" ] 
      }, 
      { 
      "name": "ZIP Code", 
      "alt": "zip", 
      "errors": [ "zip Error Message" ] 
      }, 
      { 
      "name": "Country", 
      "alt": "country" 
      }, 
      { 
      "name": "First 4 Phone Digits", 
      "alt": "first_phone" 
      }, 
      { 
      "name": "Phone Number", 
      "alt": "phone_number" 
      } 
     ] 
     }, 
     "someVal": [], 
     "someVal2": "a string" 
    } 

到目前爲止,我一直在試圖d Ø如下:

import { fromJS, Map, List } from 'immutable'; 

import form from './json/form.json' 
import config from './json/errors.json' 

let list = new List([]) 

list.merge(fromJS(config), fromJS(form)); 

console.log(list); 

console.log(list.toJS()) 
// console.log(JSON.stringify(newList, null, 2)) 

,我只得到以下輸出:

List [] 
[] 

老實說,我不知道如何開始合併這些和文檔是有很大幫助沒有。

+0

那麼,你試圖合併一個對象(第一組代碼)和一個數組(第二組代碼)。好像你真的想合併第一組代碼中的'form.fields'屬性的值和第二組代碼的數組... –

+0

你還需要告訴你想要在' alt'屬性。我建議閱讀[這個問題]的答案(http://stackoverflow.com/q/39908169/215552)瞭解更多。 –

+0

@MikeMcCaughan我嘗試過'list.merge(fromJS(errors),fromJS(form.form.fields));'但我仍然得到一個空數組。剛剛看到你的第二個評論,檢查出來。 – eveo

回答

1

首先,你需要了解不變性有

let list = new List([]) 

const mergedList = list.merge(fromJS(config), fromJS(form)); 

console.log(list); // prints empty list 

不變性指概念,即原始數據不更新(例如突變)。在你的例子中,這意味着原始的list對象仍然是空的。但.merge()會返回一個具有合併操作結果的新列表。

然後你會看到在Immutable中的列表被索引合併。看看這個簡單的例子

const a = new List([{a: 1}, {a: 2}]); 
const b = new List([{b: 3}, {b: 4}]); 

const merged = a.merge(b); 

console.log(merged.toJS()) 
// [{a: 1, b: 3}, {a: 2, b: 4}] 

如果你想不被索引,而是由你的特殊標識進行合併,你應該轉移你記錄到地圖,通過alt場鍵:

import form from './json/form.json' 
import config from './json/errors.json' 

const formByField = form.reduce(
    (total, field) => ({...total, [field.alt]: field}), 
    {} 
); 
const configByField = config.reduce(
    (total, field) => ({...total, [field.alt]: field}), 
    {} 
); 

// then you can merge two Maps together 

const result = new Map().mergeDeep(fromJS(formByField), fromJS(configByField)); 

注,你也可以使用mergeDeep爲每個匹配鍵遞歸遞歸

相關問題