简体   繁体   中英

how to pass dynamic key to json object

I need result like the following json object:

testObj = [{
  test1: "cat",
  category: "Animal"
}, {
  test2: "parrot",
  category: "Bird"
}, {
  test3: "Dog",
  category: "Animal"
}]

Create an object than push to your testObj array. Like this

obj = { test1: "cat",category: "Animal"};
testObj.push(obj);

This will create you required dynamic JSON array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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