簡體   English   中英

如何將數組屬性推送到 JS 上的 JSON object

[英]How to push an array attribute to a JSON object on JS

我有這個 json object:


 [
  {
    "id": "imrhf5owlv9j4jj0",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "HUGO",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "expense",
    "value": 0,
    "date_at": 1664147419693,
    "category_id": "",
    "cont\r\nact_id": "",
    "created_at": 1664147419687,
    "updated_at": 1664147419687
  },
  {
    "id": "san36ma2i3rs7quv",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "So queria ser eu mesmo",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "customer",
    "val\r\nue": 0,
    "date_at": 1664147602340,
    "category_id": "",
    "contact_id": "",
    "created_at": 1664147602334,
    "updated_at": 1664147602334
  },
  {
    "id": "qgscc5qe0hdtwhqh",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "SW",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "expense",
    "value": 0,
    "date_at": 1664150619834,
    "category_id": "",
    "contact_id": "",
    "created_at": 1664150619831,
    "updated_at": 1664150619831
  }
] 

我想要做的是在每個 json 中添加一個新的數組屬性,例如:


[
  {
    "id": "imrhf5owlv9j4jj0",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "HUGO",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "expense",
    "value": 0,
    "date_at": 1664147419693,
    "category_id": "",
    "cont\r\nact_id": "",
    "created_at": 1664147419687,
    "updated_at": 1664147419687
  },
  {
    "id": "san36ma2i3rs7quv",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "So queria ser eu mesmo",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "customer",
    "val\r\nue": 0,
    "date_at": 1664147602340,
    "category_id": "",
    "contact_id": "",
    "created_at": 1664147602334,
    "updated_at": 1664147602334
  },
  {
    "id": "qgscc5qe0hdtwhqh",
    "_status": "created",
    "_changed": "",
    "description": null,
    "customer_name": "SW",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "expense",
    "value": 0,
    "date_at": 1664150619834,
    "category_id": "",
    "contact_id": "",
    "created_at": 1664150619831,
    "updated_at": 1664150619831
  }, 
],
"contact": [
    "customer_name": "SW",
    "aparent_potencial": "",
    "real_potencial": "",
    "type": "expense",
    "value": 0,
    "date_at": 1664150619834,
    "category_id": "",
    "contact_id": "",
    "created_at": 1664150619831,
    "updated_at": 1664150619831
]

我試圖使用推送,但還沒有解決方案,如下所示:

changes.customer.created.push({contact: changes.contact.created})

但它在 object 中創建了一個字符串:


[{"id":"imrhf5owlv9j4jj0","_status":"created","_changed":"","description":null,"customer_name":"HUGO","aparent_potencial":"","real_potencial":"","type":"expense","value":0,"date_at":1664147419693,"category_id":"","cont
act_id":"","created_at":1664147419687,"updated_at":1664147419687},{"id":"san36ma2i3rs7quv","_status":"created","_changed":"","description":null,"customer_name":"So queria ser eu mesmo","aparent_potencial":"","real_potencial":"","type":"customer","val
ue":0,"date_at":1664147602340,"category_id":"","contact_id":"","created_at":1664147602334,"updated_at":1664147602334},{"id":"qgscc5qe0hdtwhqh","_status":"created","_changed":"","description":null,"customer_name":"SW","aparent_potencial":"","real_potencial":"","type":"expense","value":0,"date_at":1664150619834,"category_id":"","contact_id":"","created_at":1664150619831,"updated_at":1664150619831},"contact:[object Object],[object Object],[object Object]"]

另外,有沒有辦法刪除進入 json 的這種類型的字符?

cont\r\nact_id

非常感謝!

json 數組不能包含字符串鍵。 你可以這樣使用

let json = {}
json.contact = { "customer_name": "SW" }
json.array = [ {"id":1,},{"id":2,}]

Javascript 的 push function 僅在您將值推送到數組時才有效。 如果您嘗試推送到 object,它將不起作用,而是會嘗試調用不存在的“推送”鍵。 這就是為什么你得到你得到的錯誤。

確保 $scope.item 是一個數組([] 或 new Array),然后使用您想要的值推送到它。

 $scope.item = [];
 $scope.push = function () {  
   $scope.item.push({"contact":[]});
 };

您必須將其放在不同的文件中才能實現這些示例格式。 而且您的聯系人數組值不是正確的類型。 數組不包含字符串鍵。 Object {"customer_name": "SW"}或字符串"'customer_name': 'SW'"

並且您的 json 文件內容需要在 {} 或 [] 內。 但是您的示例格式不是 json 的正確格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM