Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 你能用一个位置创建一个数组吗?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 你能用一个位置创建一个数组吗?

Javascript 你能用一个位置创建一个数组吗?,javascript,html,angularjs,Javascript,Html,Angularjs,我有个问题。当JSON“Customers”作为数组出现时,我的代码可以工作但当它只有一个客户时,我的代码会创建11个表列(JSON中的键数量) JSON来自另一个应用程序,因此我无法更改它 代码: 获得客户服务 // Get the customers promises.customers.$promise.then(function (data) { if (data['Customer']) { vm.customers = data['Customer'].cus

我有个问题。当JSON“Customers”作为数组出现时,我的代码可以工作但当它只有一个客户时,我的代码会创建11个表列(JSON中的键数量)

JSON来自另一个应用程序,因此我无法更改它

代码:

获得客户服务

// Get the customers
promises.customers.$promise.then(function (data) {
    if (data['Customer']) {
        vm.customers = data['Customer'].customers;
        console.log(vm.customers);
    }
});
生成HTML就像我说的,如果是一个数组,它工作得很好,但是如果只有一个客户,它会创建11列而不是一列。

 <thead>
   <tr>
     <th></th>
     <th ng-repeat="customer in vm.customers.Customers">Customer {{$index + 1}}</th>
   </tr>
 </thead>
这个没有

    {
  "Customers": {
    "CUSTOMER_ID": "1",
    "FIRST_NAME": "Peter",
    "LAST_NAME": "Parker",
    "ACCOUNT_TYPE_DESCRIPTION": "Single Account",
    "BIRTH_DATE": "2018-06-21+00:00",
    "CUSTOMER_GENDER_DESCRIPTION": "Male",
    "STREET_ADDRESS": "Gotham Street 56",
    "POSTAL_CODE": "21312",
    "CITY": "Gotham",
    "COUNTRY_DESCRIPTION": "Portugal",
    "_xmlns": ""
  },
  "_xmlns": "http://www.infinity.com/bpm/model/CMT/Customers"
}

您可以检查它是否是数组,如果不是,则创建一个数组

promises.customers.$promise.then(function (data) {
    if (data['Customer']) {
        vm.customers = data['Customer'].customers;

        // if not an array then assume that vm.customers is actually a single
        // instance in which case create a new array 
        // with that as the 1st element
        if(!Array.isArray(vm.customers)){
            vm.customers = [vm.customers];
        }

        console.log(vm.customers);
    }
});
试试这个

promises.customers.$promise.then(function (data) {
    if (data['Customer']) {
        if( Array.isArray( data['Customer'].customers ) ) { 
            vm.customers = data['Customer'].customers;
            console.log(vm.customers);
        }else{
            vm.customers = [data['Customer'].customers];
            console.log(vm.customers);
        }
    }
});

{“Customers”:[{
在第二个示例中也应该是这样,否则你在迭代对象属性,猜猜看,它们是11。是的,我知道,但它来自另一个应用程序。我不知道如何更改,但如果我需要更改,我需要在上面的函数中执行。抱歉,我错过了巨大的“我无法更改它”signal我会试试这个。2分钟后我会给出反馈,嗯……我仍然会在HTML中得到相同的JSON。@user9973778-然后可能会有其他相同(或类似)的代码需要添加检查,或者您所做的更改没有在您的环境中执行。您可以通过添加一个附加的
控制台.log
语句来测试后者,也可以在
if
块中添加一条语句。@user9973778-如果您手动复制了上面我的解决方案中的更改,请确保在
if
块的开头
promises.customers.$promise.then(function (data) {
    if (data['Customer']) {
        if( Array.isArray( data['Customer'].customers ) ) { 
            vm.customers = data['Customer'].customers;
            console.log(vm.customers);
        }else{
            vm.customers = [data['Customer'].customers];
            console.log(vm.customers);
        }
    }
});