我正在使用 knockout.mapping 将我的 javascript 对象转换为可观察到的淘汰赛。在这个 javascript 对象内部是一个嵌套对象,这个嵌套对象正在被转换,但它位于错误的位置。因此,当我的视图尝试绑定数据时它不能,因为映射已将嵌套对象移动到根。
JSON数据
[
{
"brandName": "Knockando Single Malt Whisky",
"categoryDescription": "Alcohol - Whisky & Bourbon",
"dateAmended": "2008-01-28T10:05:00.000Z",
"defaultSearchName1": "KnockandoSingleMaltWhisky",
"defaultSearchName2": "Knockando Single Malt Whisky",
"advertiser": {
"activities": "Motor dealers",
"additionalInfo": "",
"advertiserName": "Parks of Hamilton Ltd",
"alfRank": 3311,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "Trade as Parks Motor Group.\nAlternative email: coachhire@park.uk.com, admin@park-uk.com, Info@park.uk.com.",
"numberOfEmployees": 1000,
"outdoorRank": 1426,
"pressRank": 5159,
"radioRank": 675,
"tVRank": 1810
}
},
{
"brandName": "Tesco Petrol/Diesel Fuel",
"categoryDescription": "Petrol & Service Stations",
"dateAmended": "2013-07-10T15:17:00.000Z",
"defaultSearchName1": "TescoPetrol/DieselFuel",
"defaultSearchName2": "Tesco Petrol/Diesel Fuel",
"advertiser": {
"activities": "Furnitures",
"additionalInfo": "",
"advertiserName": "Pine & Things Ltd",
"alfRank": 4570,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "Trade as P&T.\nAlternative email: info@pineandthings.co.uk.",
"numberOfEmployees": 25,
"outdoorRank": 1426,
"pressRank": 2851,
"radioRank": 2449,
"tVRank": 1810
}
},
{
"brandName": "Tesco Electrical Products",
"categoryDescription": "Supermarkets & Multiple Retailers",
"dateAmended": "2013-07-10T15:52:00.000Z",
"defaultSearchName1": "TescoElectricalProducts",
"defaultSearchName2": "Tesco Electrical Products",
"advertiser": {
"activities": "Bridalwear",
"additionalInfo": "",
"advertiserName": "Phil Collins Bridal Collection Ltd",
"alfRank": 4094,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "Alternative email: enquiries@philcollinsbridal.co.uk, pcbridal@aol.com.",
"numberOfEmployees": 30,
"outdoorRank": 1426,
"pressRank": 2487,
"radioRank": 2449,
"tVRank": 1810
}
},
{
"brandName": "Tesco Bank Insurance Ranges",
"categoryDescription": "Insurance Ranges",
"dateAmended": "2012-01-05T12:30:00.000Z",
"defaultSearchName1": "TescoBankInsuranceRanges",
"defaultSearchName2": "Tesco Bank Insurance Ranges",
"advertiser": {
"activities": "Nutition supplements",
"additionalInfo": "",
"advertiserName": "PhD Nutrition Ltd",
"alfRank": 4107,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "Alternative email: info@phd-supplements.com.\nThere is no alternative head office landline number.",
"numberOfEmployees": 30,
"outdoorRank": 1426,
"pressRank": 2500,
"radioRank": 2449,
"tVRank": 1810
}
},
{
"brandName": "Tesco Clubcard",
"categoryDescription": "Loyalty & Incentive Schemes",
"dateAmended": "2008-03-26T14:05:00.000Z",
"defaultSearchName1": "TescoClubcard",
"defaultSearchName2": "Tesco Clubcard",
"advertiser": {
"activities": "",
"additionalInfo": "",
"advertiserName": "British Regional Airlines Group PLC",
"alfRank": 6031,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "",
"numberOfEmployees": 0,
"outdoorRank": 1426,
"pressRank": 5159,
"radioRank": 2449,
"tVRank": 1810
}
},
{
"brandName": "Royal Mail Corporate",
"categoryDescription": "Public Services - Corporate",
"dateAmended": "2010-06-04T11:45:00.000Z",
"defaultSearchName1": "RoyalMailCorporate",
"defaultSearchName2": "Royal Mail Corporate",
"advertiser": {
"activities": "",
"additionalInfo": "",
"advertiserName": "Yorkshire Power Group Ltd",
"alfRank": 6031,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "",
"numberOfEmployees": 0,
"outdoorRank": 1426,
"pressRank": 5159,
"radioRank": 2449,
"tVRank": 1810
}
},
{
"brandName": "E.ON Surf & Save ISP",
"categoryDescription": "Telecom - Internet Service Providers",
"dateAmended": "2007-11-23T12:25:00.000Z",
"defaultSearchName1": "E.ONSurf&SaveISP",
"defaultSearchName2": "E.ON Surf & Save ISP",
"advertiser": {
"activities": "",
"additionalInfo": "",
"advertiserName": "Stagecoach Ltd",
"alfRank": 6031,
"cinemaRank": 286,
"directMailRank": 1291,
"internetRank": 1988,
"notes": "",
"numberOfEmployees": 0,
"outdoorRank": 1426,
"pressRank": 5159,
"radioRank": 2449,
"tVRank": 1810
}
}
]
Javascript
// Get JSON from local storage and convert to js object
var objBrands = new Object(ko.mapping.fromJSON(localStorage.getItem('Brands')));
// Convert from object to observable
brands = ko.mapping.fromJS(objBrands)
我的对象在转换为可观察之前的数据
转换后
在屏幕截图二中 - 广告客户在错误的位置。我希望它在 [0] 内,但 ko.mapping 解析了我的对象并将其放在那里。