如何把一个JSON数组中的某个对象(npm请求)

我基本上试图在请求模块中使用PUT请求在JSON数组中设置一个值。 该数组有多个主对象,我只想将其设置为其中的一个。 这是这种格式:

[ 0: { status: 'pending' } 1: { status: 'pending' } ] 

我使用'npm request'文档中的示例脚本:

 request({ method: 'PUT', preambleCRLF: true, postambleCRLF: true, uri: 'http://service.com/upload', multipart: [ { 'content-type': 'application/json', body: JSON.stringify(-what-goes-in-here-?- } ], function (error, response, body) { if (error) { return console.error('upload failed:', error); } console.log('Upload successful! Server responded with:', body); }) 

所以,最大的问题是我要把“身体”放在什么位置,例如0:{status:'approved'}而不是1:{status:'approved'}

我感谢任何帮助,如果需要,我可以详细阐述。 🙂

假设你将像这样在你的body对象中传递2个东西。

 { index: 0, // ie, object index in array status: 'approved' // ie, status to update } 

现在,你必须有upload路由处理器,如。

 app.put('/upload', function(req, res) { var index = req.body.index, status = req.body.status; // update status in your data (Array); // let's say your array is `data` data[index].status = status; // updated. res.send('success'); // etc. })