如何在JavaScript中从字符串解析数组?
我的如何在JavaScript中从字符串解析数组?,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我的curl调用看起来像 curl -v -H 'Content-Type:application/json' -d '{"instanceIds": "[i-081ec3ffa72eb338, i-0c7474fb67bb9043]", "region": "us-west-2a"}' http://localhost:3001/instances/delete app.post('/instances/delete', function (req, res) { let {regio
curl
调用看起来像
curl -v -H 'Content-Type:application/json' -d '{"instanceIds": "[i-081ec3ffa72eb338, i-0c7474fb67bb9043]", "region": "us-west-2a"}' http://localhost:3001/instances/delete
app.post('/instances/delete', function (req, res) {
let {region, instanceIds} = req.body;
console.log(region + "," + instanceIds);
stopInstances(region, instanceIds)
.then(function (data) {
res.send(data);
})
.catch(function (reason) {
res.status(500).send("Error in deleting instances: " + reason);
});
});
export let stopInstances = function (region, instanceIds) {
let params = {InstanceIds: instanceIds, DryRun: true};
console.log(params);
return ec2.stopInstances(params).promise();
};
在我的基于expressJS
的服务器上,端点看起来像
curl -v -H 'Content-Type:application/json' -d '{"instanceIds": "[i-081ec3ffa72eb338, i-0c7474fb67bb9043]", "region": "us-west-2a"}' http://localhost:3001/instances/delete
app.post('/instances/delete', function (req, res) {
let {region, instanceIds} = req.body;
console.log(region + "," + instanceIds);
stopInstances(region, instanceIds)
.then(function (data) {
res.send(data);
})
.catch(function (reason) {
res.status(500).send("Error in deleting instances: " + reason);
});
});
export let stopInstances = function (region, instanceIds) {
let params = {InstanceIds: instanceIds, DryRun: true};
console.log(params);
return ec2.stopInstances(params).promise();
};
函数stopInstances
如下所示
curl -v -H 'Content-Type:application/json' -d '{"instanceIds": "[i-081ec3ffa72eb338, i-0c7474fb67bb9043]", "region": "us-west-2a"}' http://localhost:3001/instances/delete
app.post('/instances/delete', function (req, res) {
let {region, instanceIds} = req.body;
console.log(region + "," + instanceIds);
stopInstances(region, instanceIds)
.then(function (data) {
res.send(data);
})
.catch(function (reason) {
res.status(500).send("Error in deleting instances: " + reason);
});
});
export let stopInstances = function (region, instanceIds) {
let params = {InstanceIds: instanceIds, DryRun: true};
console.log(params);
return ec2.stopInstances(params).promise();
};
但是,这些值打印为string
InstanceIds: '[i-081ec3ffa72eb338, i-0c7474fb67bb9043]'
如何将其转换为数组
?我尝试了Array.from
,但它会解析每个字符
Array.from('[i-081ec3ffa72eb338, i-0c7474fb67bb9043]')
(40) ["[", "i", "-", "0", "8", "1", "e", "c", "3", "f", "f", "a", "7", "2", "e", "b", "3", "3", "8", ",", " ", "i", "-", "0", "c", "7", "4", "7", "4", "f", "b", "6", "7", "b", "b", "9", "0", "4", "3", "]"]
但是我想要[I-081ec3ffa72eb338,I-0c7474fb67bb9043]
谢谢我的
curl
呼叫错误,根据georg
的评论,我将其修复为
curl -v -H 'Content-Type:application/json' -d '{"instanceIds": ["i-081ec3ffa72eb338", "i-0c7474fb67bb9043"], "region": "us-west-2a"}' http://localhost:3001/instances/delete
这就解决了问题。您可以用逗号分隔字符串
var parts = your_string.split(",")
然后清洁零件,即拆下支架
var cleaned = parts.map( function (part) {
return part.replace(/[]/g, "");
});
清理后的变量现在应该具有所需的部分
未经测试,并且是从移动设备编写的,因此您可能需要重构该方法。这是一个字符串,因为您传递了一个字符串,您可能需要
{“InstanceId”:[“i-081ec3ffa72eb338”,“i-0C74FB67BB9043”]…
在您的请求中。谢谢您的锐利目光!我完全傻了。我觉得可以随意删除问题而不是回答,只需删除即可。这对除您之外的其他人没有用处