简体   繁体   English

如何遍历数组并仅提取特定值的键:值?

[英]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.

相关问题 如何遍历对象数组并在React中获取特定的键值? - How to loop through an array of objects and get specific key values in React? 遍历对象的对象并提取等于特定值的键值? - Loop through an object of objects and extract key value that equals something specific? 如何遍历一个数组(递归)并将对象键/值对推入另一个数组中? - How do I loop through an array (recurision) and push object key/value pairs in another array? 如何遍历并从复杂对象(数组)中提取属性值? - How to loop through and extract property values from a complex object (Array)? 如何遍历嵌套键值对并根据数组值对其进行过滤 - How to loop through a nested key value pair and filter it based on a array values 我如何遍历并向值添加键值 - How do I loop through and add key values to values 如何从 object 中获取 object.key 数组(不是值,不是字符串,不仅是键)? - How do you get an array of object.key(not values, not string, not only keys) from an object? JavaScript:从键值数组中提取特定键的值并将它们存储在新数组中 - JavaScript: Extract values of a specific key out of key-value array and store them in a new array 仅通过特定键、值对的错误值过滤数组 - Filtering array by only false values of a specific key, value pair 如何对必须与特定键匹配的值和另一个数组中的相同值的数组进行foreach操作 - How to do a foreach for an array with values that have to match specific key and the same value from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM