[英]How do you loop through an array and only extract key:value that are specific values?
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 .如果 "haslyrics" 为 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.
感谢@StepUp 和@Ankita。
I needed to keep the if-else statements.我需要保留 if-else 语句。 The lyricsData is dynamic and sometimes the length is 0. In that case, I need to pass a null through a function.
歌词数据是动态的,有时长度为 0。在这种情况下,我需要通过函数传递一个空值。
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:只需使用
filter
方法:
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
.只需使用
Array.filter()
来过滤具有haslyrics
值为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 .问题是您在
for
签名中犯了一个容易出错的错误, 文档 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:您使用的第二个参数不返回整数(数字),
lyricsData.result
是一个数组,您要返回其.length
,即:
for (let i=0; i<lyricsData.result.length; i++) {
...
}
However, I would advise you to use .filter
as others have already suggested.但是,我建议您像其他人已经建议的那样使用
.filter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.