Return REQUEST nodejs中JSON body的特定变量
Return a specific variable of a JSON body in REQUEST nodejs
我有这段代码非常适合我,它给我带来 JSON 中的 body,但我不想要整个 body,我想要一个特定的相同 body 的变量,这是代码。
var request = require("request");
var options = {
method: 'GET',
url: 'https://xxx',
qs: {stats: 'true', events: 'true'},
headers: {
'x-rapidapi-host': 'xx',
'x-rapidapi-key': 'xxx'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
**console.log('HOME NOMBRE: ' + body.results.id);**
});
这给了我一个像这样的 JSON 文件:
{"results":[{"id":1,
"idSeason":949,
"seasonName":"2020",
"idHome":2069,
"homeName":"MyHome",
"idAway":207 ....}
我希望能够实现一些方法来专门为我提供变量,例如 homeName,以便只能使用它!
希望我解释得很好,希望得到您的帮助!
如果您的 API returns 一个对象数组,而您只想要一个对象属性的值数组,请使用 map...
let results = [{
"id": 1,
"idSeason": 949,
"seasonName": "2020",
"idHome": 2069,
"homeName": "MyHome",
"idAway": 207
}, {
"id": 2,
"idSeason": 950,
"seasonName": "2021",
"idHome": 2070,
"homeName": "MyHome2",
"idAway": 208
}]
// native
console.log(results.map(e => e.homeName))
// or using underscore
console.log(_.pluck(results, 'homeName'))
// or a smaller object
console.log(results.map(e => _.pick(e, 'id', 'seasonName', 'idAway')))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
我认为更好的方法是使用 ES6 映射。
如果您使用对象数组
const results = [{
"id": 1,
"idSeason": 949,
"seasonName": "2020",
"idHome": 2069,
"homeName": "MyHome",
"idAway": 207
}, {
"id": 2,
"idSeason": "x",
"seasonName": "x",
"idHome": "x",
"homeName": "MyHomex",
"idAway": "x"
}]
const formattedRes = results.map(singleObject =>{
// Here redeclare the object you want, here i want to return id,seasonName and homeName
return {
"id": singleObject.id,
"seasonName": singleObject.seasonName,
"homeName": singleObject.seasonName,
}
});
console.log(formattedRes);
结果将是
[ { id: 1, seasonName: '2020', homeName: '2020' },
{ id: 2, seasonName: 'x', homeName: 'x' } ]
我有这段代码非常适合我,它给我带来 JSON 中的 body,但我不想要整个 body,我想要一个特定的相同 body 的变量,这是代码。
var request = require("request");
var options = {
method: 'GET',
url: 'https://xxx',
qs: {stats: 'true', events: 'true'},
headers: {
'x-rapidapi-host': 'xx',
'x-rapidapi-key': 'xxx'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
**console.log('HOME NOMBRE: ' + body.results.id);**
});
这给了我一个像这样的 JSON 文件:
{"results":[{"id":1,
"idSeason":949,
"seasonName":"2020",
"idHome":2069,
"homeName":"MyHome",
"idAway":207 ....}
我希望能够实现一些方法来专门为我提供变量,例如 homeName,以便只能使用它!
希望我解释得很好,希望得到您的帮助!
如果您的 API returns 一个对象数组,而您只想要一个对象属性的值数组,请使用 map...
let results = [{
"id": 1,
"idSeason": 949,
"seasonName": "2020",
"idHome": 2069,
"homeName": "MyHome",
"idAway": 207
}, {
"id": 2,
"idSeason": 950,
"seasonName": "2021",
"idHome": 2070,
"homeName": "MyHome2",
"idAway": 208
}]
// native
console.log(results.map(e => e.homeName))
// or using underscore
console.log(_.pluck(results, 'homeName'))
// or a smaller object
console.log(results.map(e => _.pick(e, 'id', 'seasonName', 'idAway')))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
我认为更好的方法是使用 ES6 映射。
如果您使用对象数组
const results = [{
"id": 1,
"idSeason": 949,
"seasonName": "2020",
"idHome": 2069,
"homeName": "MyHome",
"idAway": 207
}, {
"id": 2,
"idSeason": "x",
"seasonName": "x",
"idHome": "x",
"homeName": "MyHomex",
"idAway": "x"
}]
const formattedRes = results.map(singleObject =>{
// Here redeclare the object you want, here i want to return id,seasonName and homeName
return {
"id": singleObject.id,
"seasonName": singleObject.seasonName,
"homeName": singleObject.seasonName,
}
});
console.log(formattedRes);
结果将是
[ { id: 1, seasonName: '2020', homeName: '2020' },
{ id: 2, seasonName: 'x', homeName: 'x' } ]