简体   繁体   English

如何将数组属性推送到 JS 上的 JSON object

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

I have this json object:我有这个 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
  }
] 

What I want to do is to add a new array attribute inside each of the json, so, for example:我想要做的是在每个 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
]

I was trying to use push, but no solution yet, as below:我试图使用推送,但还没有解决方案,如下所示:

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

but it created a string inside the object:但它在 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]"]

Also, is there a way to remove this type of char that gets into the json?另外,有没有办法删除进入 json 的这种类型的字符?

cont\r\nact_id

Thanks so much!非常感谢!

json array cannot contains string key. json 数组不能包含字符串键。 you can use like this你可以这样使用

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

Javascript's push function only works when you're pushing values to an array. Javascript 的 push function 仅在您将值推送到数组时才有效。 It won't work if you try to push to an object, instead it will try to call the key of "push" which doesn't exist.如果您尝试推送到 object,它将不起作用,而是会尝试调用不存在的“推送”键。 That's why you're getting the error you're getting.这就是为什么你得到你得到的错误。

Make sure that $scope.item is an array ([] or new Array), and then push to it with the value you would like.确保 $scope.item 是一个数组([] 或 new Array),然后使用您想要的值推送到它。

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

You have to put it in a different file to achieve those example format.您必须将其放在不同的文件中才能实现这些示例格式。 And your contacts array value is not the correct type.而且您的联系人数组值不是正确的类型。 Array does not contain string key.数组不包含字符串键。 Either Object {"customer_name": "SW"} or string "'customer_name': 'SW'" . Object {"customer_name": "SW"}或字符串"'customer_name': 'SW'"

And your json file content need to be inside {} or [].并且您的 json 文件内容需要在 {} 或 [] 内。 But your example format is not the correct format for json.但是您的示例格式不是 json 的正确格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM