0

I have the following result from database (using sequelize), the result:

JSON from the database:

 [{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]

In the controller I do the next:

 vm.spareParts = data.data;
            console.log('-------- ' + JSON.stringify(vm.spareParts));
            for (var i = 0; i < vm.spareParts.length; i++) {
                vm.items.push({
                    codSparePart: vm.spareParts[i].codSparePart,
                    name: vm.spareParts[i].name,
                    description: vm.spareParts[i].description,
                    available: vm.spareParts[i].available,
                    codManufacturer: vm.spareParts[i].codManufacturer,
                    nameManufacturer: vm.sparePart[i]['manufacturer.description'],
                    stock: vm.spareParts[i].stock,
                    codProject: vm.spareParts[i].codProject
                });

All is working perfectly except in reading the variables with dot. nameManufacturer: vm.sparePart[i]['manufacturer.description'] nameManufacturer: vm.sparePart[i]['manufacturer.name']

How can I read these variables, I'm looking for the solution but I think is too simple and there is not any information about it. Thanks in advance for your help.

Miguel-Ángel
  • 57
  • 2
  • 11

2 Answers2

0

Your code seems to be working perfectly:

const array =[{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]

console.log(array[0]['manufacturer.description']);

More information: How to get JSON objects value if its name contains dots?

Zooly
  • 4,736
  • 4
  • 34
  • 54
0

You have a typo, change vm.sparePart to vm.spareParts

This line: nameManufacturer: vm.sparePart[i]['manufacturer.description'],

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • Thank you, stupid question for stupid error. I`m sorry for the question. I'm a beginner and some times the things are not clear for me. – Miguel-Ángel Apr 04 '18 at 16:49