Home > front end >  How to push an array attribute to a JSON object on JS
How to push an array attribute to a JSON object on JS

Time:09-26

I have this 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:


[
  {
    "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:


[{"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?

cont\r\nact_id

Thanks so much!

CodePudding user response:

json array cannot contains string key. you can use like this

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

CodePudding user response:

Javascript's push function only works when you're pushing values to an array. 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. 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 = [];
 $scope.push = function () {  
   $scope.item.push({"contact":[]});
 };

CodePudding user response:

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'".

And your json file content need to be inside {} or []. But your example format is not the correct format for json.

  • Related