AngularJS:使用带有点的变量读取数组

时间:2018-04-04 15:11:13

标签: arrays angularjs json

我从数据库得到以下结果(使用sequelize),结果:

数据库中的JSON:

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

在控制器中我执行下一步:

 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
                });

除了用点读取变量外,所有工作都完美无缺。       nameManufacturer: vm.sparePart[i]['manufacturer.description'] nameManufacturer: vm.sparePart[i]['manufacturer.name']

我如何阅读这些变量,我正在寻找解决方案,但我认为太简单了,而且没有任何相关信息。 在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的代码似乎运作正常:

&#13;
&#13;
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']);
&#13;
&#13;
&#13;

更多信息:How to get JSON objects value if its name contains dots?

答案 1 :(得分:0)

您输入错误,将vm.sparePart更改为vm.spareParts

  

这一行:nameManufacturer: vm.sparePart[i]['manufacturer.description'],