【问题标题】:How to add a value to a JSON array of objects?如何向 JSON 对象数组添加值?
【发布时间】:2017-08-04 19:00:58
【问题描述】:

我有一个这样的对象的 json 数组,用于将值传递给数据表。

[{ _id: '58a2b5941a9dfe3537aad540',
    Country: 'India',
    State: 'Andhra Pradesh',
    District: 'Guntur',
    Division: '',
    Village: 'Macharla',
    FarmerName: 'Vijay',
    Address: '',
    Pin: '',
    PrimaryContact: '9160062222',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad541',
    Country: 'India',
    State: 'Telangana',
    District: 'Karimnagar',
    Division: '',
    Village: 'Sirisilla',
    FarmerName: 'Subhash Rao',
    Address: '',
    Pin: '',
    PrimaryContact: '8121787878/9441967878',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad542',
    Country: 'India',
    State: 'Telangana',
    District: 'Medak',
    Division: '',
    Village: 'Jagur',
    FarmerName: 'Ramachandra',
    Address: '',
    Pin: '',
    PrimaryContact: '9346481116',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad543',
    Country: 'India',
    State: 'Telangana',
    District: 'Mahaboob Nagar',
    Division: '',
    Village: 'annugal',
    FarmerName: 'Rajesh Reddy',
    Address: '',
    Pin: '',
    PrimaryContact: '93464593808',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' } ]

现在我想在这个数组之外再添加一个对象,例如“recordsTotal”:600。我怎样才能实现它? 最后我想要这样的数据

{
    "draw": 1,
    "recordsTotal": 1,
    "recordsFiltered": 1,
    "data": [{
    "VendorID": "V0000051",
    "Name": "STAPLES CONTRACT AND COMMERCIAL INC",
    "Mnemonic": "CORPORATE",
    "TermsDescription": "INV 2/30 NET 31",
    "ActiveYn": "Y"
    }]
}

【问题讨论】:

  • 提示:使用JSON.parse,然后像处理任何其他 JS 对象一样操作它...

标签: javascript json node.js


【解决方案1】:

首先将json数组存储在一个变量中,比如

var datalist=[{ _id: '58a2b5941a9dfe3537aad540',
    Country: 'India',
    State: 'Andhra Pradesh',
    District: 'Guntur',
    Division: '',
    Village: 'Macharla',
    FarmerName: 'Vijay',
    Address: '',
    Pin: '',
    PrimaryContact: '9160062222',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad541',
    Country: 'India',
    State: 'Telangana',
    District: 'Karimnagar',
    Division: '',
    Village: 'Sirisilla',
    FarmerName: 'Subhash Rao',
    Address: '',
    Pin: '',
    PrimaryContact: '8121787878/9441967878',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad542',
    Country: 'India',
    State: 'Telangana',
    District: 'Medak',
    Division: '',
    Village: 'Jagur',
    FarmerName: 'Ramachandra',
    Address: '',
    Pin: '',
    PrimaryContact: '9346481116',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' },
  { _id: '58a2b5941a9dfe3537aad543',
    Country: 'India',
    State: 'Telangana',
    District: 'Mahaboob Nagar',
    Division: '',
    Village: 'annugal',
    FarmerName: 'Rajesh Reddy',
    Address: '',
    Pin: '',
    PrimaryContact: '93464593808',
    OtherContacts: '',
    Email: '',
    updatedAt: '2017-02-14T04:39:01.000Z',
    modifiedBy: '',
    createdAt: '2017-02-14T04:39:01.000Z' } ]

然后声明另一个json对象并初始化像

这样的值
var FinalJsonObject={"draw": 1,
    "recordsTotal": 1,
    "recordsFiltered": 1,
    "data": datalist}

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 2012-08-21
    • 2019-11-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多