I have a JSON Array that includes various rules and values, but I want to convert it into a specific array format. Here is an example of the original JSON Array:
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}
The goal is to convert it into the following array:
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}
Each item in the new array should have a label
key and display the values of clean
and copy
.
However, when I try to achieve this using a JavaScript function, the result is not as expected. Instead of separate items, everything is combined into one object. Here is the code snippet I used:
var allItems = [data.Mappings];
var testData = allItems.map(function(item){
return {
'label': item,
'clean': item.Clean,
'copy': item.Copy
};
});
console.log(testData)
But this approach doesn't work as intended, and the output is like this:
0:
clean: undefined
copy: undefined
label: {OwnershipRule: {…}, BoardParticipationRule: {…}, SkillsDevelopmentRule: {…}, EmployeeStructureRule: {…}, PreferentialProcurementRule: {…}, …}
I can't figure out what I'm doing wrong here. Any suggestions?