在knockout中将JSON数据转换为Javascript数组
Convert JSON data to Javascript Array in knockout
我有以下 JSON 数组:
AccessToFinancialServicesRule: {Clean: 3, Copy: 3}
BoardParticipationRule: {Clean: 3, Copy: 3}
Documents: null
EconomicDevelopmentRule: {Clean: 3, Copy: 3}
EmployeeStructureRule: {Clean: 3, Copy: 3}
EmpowermentFinanceRule: {Clean: 3, Copy: 3}
EnterpriseDevelopmentRule: {Clean: 3, Copy: 3}
OwnershipRule: {Clean: 3, Copy: 3}
PreferentialProcurementRule: {Clean: 3, Copy: 3}
ResponsibleSocialMarketingRule: {Clean: 3, Copy: 3}
SkillsDevelopmentRule: {Clean: 3, Copy: 3}
SocioEconomicDevelopmentRule: {Clean: 3, Copy: 3}
SupplierDevelopmentRule: {Clean: 3, Copy: 3}
我正在尝试将其转换成如下所示的数组
0: {label: "Ownership", clean: 3, copy: 3}
1: {label: "Skills<br>Development", clean: 3, copy: 3}
2: {label: "Localisation", clean: 3, copy: 3}
3: {label: "Socio-Economic<br>Development", clean: 3, copy: 3}
4: {label: "Board Participation", clean: 3, copy: 3}
5: {label: "Employee Structure", clean: 3, copy: 3}
6: {label: "Preferential Procurement", clean: 3, copy: 3}
7: {label: "Enterprise Development", clean: 3, copy: 3}
8: {label: "Supplier Development", clean: 3, copy: 3}
其中每个项目称为 label
并显示 clean
和 copy
我试过以下方法:
var allItems = [data.Mappings];
var testData = allItems.map(function(item){
return {
'label': item,
'clean': item.Clean,
'copy':item.Copy
};
});
console.log(testData)
但这似乎行不通,它把所有东西都变成了一个对象,就像这样:
0:
clean: undefined
copy: undefined
label: {OwnershipRule: {…}, BoardParticipationRule: {…}, SkillsDevelopmentRule: {…}, EmployeeStructureRule: {…}, PreferentialProcurementRule: {…}, …}
我做错了什么?
您需要使用 Object.keys
来访问对象中的属性,例如:
const convertToArray = array.map((item) => {
const key = Object.keys(item)
if (item[key]) {
return {
label: key[0],
clean: item[key].Clean,
copy: item[key].Copy
}
}
})
检查 link 以获取完整示例:https://jsfiddle.net/qgwxbuem/
我有以下 JSON 数组:
AccessToFinancialServicesRule: {Clean: 3, Copy: 3}
BoardParticipationRule: {Clean: 3, Copy: 3}
Documents: null
EconomicDevelopmentRule: {Clean: 3, Copy: 3}
EmployeeStructureRule: {Clean: 3, Copy: 3}
EmpowermentFinanceRule: {Clean: 3, Copy: 3}
EnterpriseDevelopmentRule: {Clean: 3, Copy: 3}
OwnershipRule: {Clean: 3, Copy: 3}
PreferentialProcurementRule: {Clean: 3, Copy: 3}
ResponsibleSocialMarketingRule: {Clean: 3, Copy: 3}
SkillsDevelopmentRule: {Clean: 3, Copy: 3}
SocioEconomicDevelopmentRule: {Clean: 3, Copy: 3}
SupplierDevelopmentRule: {Clean: 3, Copy: 3}
我正在尝试将其转换成如下所示的数组
0: {label: "Ownership", clean: 3, copy: 3}
1: {label: "Skills<br>Development", clean: 3, copy: 3}
2: {label: "Localisation", clean: 3, copy: 3}
3: {label: "Socio-Economic<br>Development", clean: 3, copy: 3}
4: {label: "Board Participation", clean: 3, copy: 3}
5: {label: "Employee Structure", clean: 3, copy: 3}
6: {label: "Preferential Procurement", clean: 3, copy: 3}
7: {label: "Enterprise Development", clean: 3, copy: 3}
8: {label: "Supplier Development", clean: 3, copy: 3}
其中每个项目称为 label
并显示 clean
和 copy
我试过以下方法:
var allItems = [data.Mappings];
var testData = allItems.map(function(item){
return {
'label': item,
'clean': item.Clean,
'copy':item.Copy
};
});
console.log(testData)
但这似乎行不通,它把所有东西都变成了一个对象,就像这样:
0:
clean: undefined
copy: undefined
label: {OwnershipRule: {…}, BoardParticipationRule: {…}, SkillsDevelopmentRule: {…}, EmployeeStructureRule: {…}, PreferentialProcurementRule: {…}, …}
我做错了什么?
您需要使用 Object.keys
来访问对象中的属性,例如:
const convertToArray = array.map((item) => {
const key = Object.keys(item)
if (item[key]) {
return {
label: key[0],
clean: item[key].Clean,
copy: item[key].Copy
}
}
})
检查 link 以获取完整示例:https://jsfiddle.net/qgwxbuem/