{
id: 23323091,
userId: 1743514,
teamId: 1693131,
name: 'test1'
},
{
id: 2332950,
userId: 1743514,
teamId: 1693131,
name: 'test2'
},
{
id: 23323850,
userId: 1743514,
teamId: 1693131,
name: 'test3'
}
Im trying find solution how to parse json result returned from GET request in node.js. So I want find in json array with ex. name: 'test3' return id:
Here is full code:
const axios = require('axios');
let token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0MzUxNCwidXNlcm5hbWUiOiJzbmFwa2l0cmljaEBnbWFpbC5jb20iLCJyb2xlIjoiYWRtaW4iLCJ0ZWFtSWQiOjE2OTMxMzEsInRv';
axios.get('https://dolphin-anty-api.com/browser_profiles', {
headers: {
Authorization: `Bearer ${token}`
}
})
.then(function (response) {
const data = response.data;
console.log(data);
})
.catch(function (error) {
console.log(error);
});
and here is full data response from console log https://pastebin.com/wY0RVsUv
CodePudding user response:
You can use the Array#find method
const elements = [
{
id: 23323091,
userId: 1743514,
teamId: 1693131,
name: 'test1'
},
{
id: 2332950,
userId: 1743514,
teamId: 1693131,
name: 'test2'
},
{
id: 23323850,
userId: 1743514,
teamId: 1693131,
name: 'test3'
}
]
function search(name) {
return elements.find(elem => elem.name == name)?.id
}
console.log(search('test3'))
console.log(search('test4'))
CodePudding user response:
If you are inside a GET request, to return a Json just do response.json()
Store your JSON inside a const
for example
const data = myParsedJsonObject
You can find whatever data you looking for using the ES6 find()
method.
const names = data.find(el => el.name === "test3")