无法将多个元素附加到数组对象中

时间:2017-07-03 05:29:02

标签: javascript arrays ionic2

这是我需要更新的数组“serviceSubCatNamesToListToSet”

{
"serviceTypes": [
    {
        "service_type_id": 1,
        "service_type_name": "Washing",
        "service_type_price": "3000.00",
        "created_at": null,
        "updated_at": null,
        "pivot": {
            "service_service_type_service_id": 1,
            "service_service_type_service_type_id": 1
        }
    }
 ]
}

我需要将一个名为“technicianData”的数组对象附加到上面的(serviceSubCatNamesToListToSet)数组中。

我追加“technicianData”的方式

  this.toServiceSubCatNamesList[this.technicianIndex]['technicianData'] = this.technicianData;

追加“serviceSubCatNamesToListToSet”之后会是这样的。

serviceSubCatNamesToListToSet:
   > Object
     created_at:null
     pivot:Object
     service_type_id:1
     service_type_name:"Washing"
     service_type_price:"3000.00"
     >technicianData:Object
                 created_at:null
                 technician_address:"Colombo 05"
                 technician_code:"T0001"
                 technician_id:1
                 technician_name:"Brian Adams"
                 technician_tel1:"312252937"

到目前为止,它正在工作。但是当我尝试将多个元素添加到“serviceSubCatNamesToListToSet.technicianData”时,它无效。

任何克服此问题的建议!谢谢!

1 个答案:

答案 0 :(得分:0)

假设serviceTypes是原始的json数组,serviceTypes[0]将允许获取第一个对象



var serviceTypes= [{
  "service_type_id": 1,
  "service_type_name": "Washing",
  "service_type_price": "3000.00",
  "created_at": null,
  "updated_at": null,
  "pivot": {
    "service_service_type_service_id": 1,
    "service_service_type_service_type_id": 1
  }
}]
var technicianData = [1,2,3];
serviceTypes[0].technicianData=technicianData;
console.log(serviceTypes)