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 []
[]
老實說,我不知道如何開始合併這些和文檔是有很大幫助沒有。
那麼,你試圖合併一個對象(第一組代碼)和一個數組(第二組代碼)。好像你真的想合併第一組代碼中的'form.fields'屬性的值和第二組代碼的數組... –
你還需要告訴你想要在' alt'屬性。我建議閱讀[這個問題]的答案(http://stackoverflow.com/q/39908169/215552)瞭解更多。 –
@MikeMcCaughan我嘗試過'list.merge(fromJS(errors),fromJS(form.form.fields));'但我仍然得到一個空數組。剛剛看到你的第二個評論,檢查出來。 – eveo