我有一个正在使用API的函数,并且无需支付JSON
我想要的是仅过滤JSON中的某些特定元素,而不是所有元素 我怎样才能做到这一点 ?
预先感谢您的帮助
function getAllIssueForSCII(){
var options = {
method: 'GET',
url: '***',
headers: {
'Accept': 'application/json',
'Authorization': "***"
}
};
return new Promise(function (resolve) {
request(options, function (error, response, data) {
if (!error && response.statusCode === 200) {
console.log('JIRA login success!!')
var json = JSON.parse(data);
console.log(json); // display all element JSON
resolve(json);
} else {
console.log('error: ' + response.statusCode + ': ' + data.statusMessage)
}
})
})
实际结果:
{
expand: 'schema,names',
startAt: 0,
maxResults: 50,
total: 2,
issues: [{
expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
id: '59032',
self: '**',
key: 'SCII-10',
fields: [Object]
},
{
expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
id: '59028',
self: '**',
key: 'SCII-7',
fields: [Object]
}
]
}
预期结果:
{
issues: [{
key: 'SCII-10',
fields: id: 1
name: toto
time: now
},
答案 0 :(得分:1)
@Danmoreng在其评论中提到,您只需要浏览JSON。
我想您是javascript新手,让我告诉您:
var jj = { expand: 'schema,names',
startAt: 0,
maxResults: 50,
total: 2,
issues:
[ { expand:
'operations,versionedRepresentations,editmeta,changelog,renderedFields',
id: '59032',
self: '**',
key: 'SCII-10',
fields: [Object] },
{ expand:
'operations,versionedRepresentations,editmeta,changelog,renderedFields',
id: '59028',
self: '**',
key: 'SCII-7',
fields: [Object] } ] } ;
var expected_output = {}; // we declare an empty object
expected_output.issues = []; // we declare "issues" a new field in our object
expected_output.issues[0] = {}; // we first element of our array is an object
expected_output.issues[0].key = jj.issues[0].key;
expected_output.issues[0].fields = [];
expected_output.issues[0].fields = jj.issues[0].fields;
console.log(expected_output);
答案 1 :(得分:0)
应该执行以下操作:
const response = { expand: 'schema,names', startAt: 0, maxResults: 50, total: 2, issues: [ { expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields', id: '59032', self: '**', key: 'SCII-10', fields: {} }, { expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields', id: '59028', self: '**', key: 'SCII-7', fields: {} } ] };
let filterId = '59032';
console.log("Filtered Issues: ", { issues: response.issues.filter(issue => issue.id === filterId ) });