I would like to loop through an existing object and only extract specific keys to add to a new array.
The object looks like:
let lyricsData = {
"success": true;
"length": 50;
"result": [
{
"id_track": 123,
"haslyrics": true,
"id_artrist": 234,
},
{
"id_track": 567,
"haslyrics": false,
"id_artrist": 678,
}
]
}
I would only like to extract the results if "haslyrics" is true .
This is the block of code I've come up with:
1 function findHasLyrics(lyricsData) {
2 if (lyricsData.length === 0) {
3 console.log("findHasLyrics", null);
4 } else {
5 let hasLyricsTrue = [];
6 for (let i=0; i<lyricsData.length; i++) {
7 if (lyricsData.result[i].haslyrics === true) {
8 hasLyricsTrue.push(lyricsData.result[i]);
9 };
10 console.log("findHasLyrics", hasLyricsTrue);
11 };
12 };
13 };
Where is the code breaking? And how would I fix it?
Thanks in advance.
SOLUTION (EDITED). Thanks @StepUp and @Ankita.
I needed to keep the if-else statements. The lyricsData is dynamic and sometimes the length is 0. In that case, I need to pass a null through a function.
function findHasLyrics(lyricsData) {
if (lyricsData.length === 0) {
displayLyricsApi(null);
} else {
function findHasLyrics(lyricsData) {
return lyricsData.result.filter(f => f.haslyrics)
};
formatQueryLyrics(findHasLyrics(lyricsData));
};
}
Just use filter
method:
lyricsData.result.filter(f=> f.haslyrics)
in your case:
function findHasLyrics(lyricsData) {
return return lyricsData.result.filter(f=> f.haslyrics);
};
An example:
var data = { "success": true, "length": 50, "result": [{ "id_track": 123, "haslyrics": true, "id_artrist": 234, }, { "id_track": 567, "haslyrics": false, "id_artrist": 678, } ] }; function findHasLyrics(lyricsData) { return lyricsData.result.filter(f=> f.haslyrics) }; console.log(findHasLyrics(data));
Just use Array.filter()
to filter the array with those objects that has haslyrics
value as true
. You can avoid the custom loops and logic with that function.
var obj = { "success": true, "length": 50, "result": [ { "id_track": 123, "haslyrics": true, "id_artrist": 234, }, { "id_track": 567, "haslyrics": false, "id_artrist": 678, } ] }; var res = obj.result.filter(({haslyrics}) => haslyrics); console.log(res);
In your code you can update this way:
function findHasLyrics(lyricsData) { if (lyricsData.length === 0) { console.log("findHasLyrics", null); } else { var res = lyricsData.result.filter(({haslyrics}) => haslyrics); console.log(res); }; }; var obj = { "success": true, "length": 50, "result": [{ "id_track": 123, "haslyrics": true, "id_artrist": 234, }, { "id_track": 567, "haslyrics": false, "id_artrist": 678, } ] }; findHasLyrics(obj);
The problem is that you've made an easy to make mistake in the for
signature, docs here .
The second argument you're using doesn't return an Integer (number), lyricsData.result
is an array, you want to return its .length
, ie:
for (let i=0; i<lyricsData.result.length; i++) {
...
}
However, I would advise you to use .filter
as others have already suggested.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.