简体   繁体   English

如何从javascript中的对象数组显示对象的所有属性?

[英]How to display all the properties of an object from an array of objects in javascript?

I am new to javascript.I have been working on a program where I use a json dataset in javascript by assigning it to a variable.The link of the dataset is below. 我是javascript新手,我一直在研究一个程序,在其中我通过将javascript的json数据集分配给变量来使用它。数据集的链接如下。 https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json

I have to write a function where I take a pokemon's name as input and display all its details.Below is the function I used. 我必须编写一个函数,以口袋妖怪的名字作为输入并显示其所有详细信息。下面是我使用的函数。

var list=pokeDex.pokemon;
var name=prompt("Enter the name of a pokemon");
// Function  taking pokemon’s name as argument and displaying the 
information of that pokemon.
function displayInfo(name)
 {
  for(var items in list)
  {
   if (name==list[items].name)
   {
   console.log(list[items]);
   break;
    }

    else
    {
    //alert("error");

    }
   }
  }

The function seems to be working properly for all the pokemons in the list only if the alert statement in else block is a comment.Otherwise the function displays error for all the pokemons except the first one in the dataset. 仅当else块中的alert语句为注释时,该函数才能对列表中的所有神奇宝贝正常工作,否则该函数将显示除数据集中的第一个之外的所有神奇宝贝的错误。

You can easily simplyfy your logic to 您可以轻松地根据自己的逻辑

function displayInfo(searchName)
 {
  const filteredList = list.filter(poke => poke.name==searchName)
   if (filteredList > 0)
   {
   console.log(filteredList[0]);
    }
    else
    {
    alert("error");
    }
  }

This will first iterate through list and search for the pokemon with searchName 这将首先遍历列表并使用searchName搜索pokemon

In your code it tries to give error alert on if first one is not the searchPokemon ieelse block without even searching in rest of list. 在您的代码中,如果第一个不是searchPokemon ieelse块,它甚至在列表的其余部分中进行搜索时都会尝试给出错误警告。

Hi simplify your logic by filtering by name, there are high order functions like filter that iterate through arrays and gets the one that returns true to your condition, in this case, the condition would be the pokemon name. 嗨,您可以通过按名称过滤来简化您的逻辑,其中有一些高阶函数,例如filter ,可以遍历数组并获得返回条件为true的函数,在这种情况下,条件就是神奇宝贝的名字。 Also when matching strings it's interesting to apply lowercase to match regardless of uppercase or lowercase letters 同样,在匹配字符串时,不管大小写字母如何,都应用小写字母进行匹配很有趣

Code could look like the following: 代码如下所示:

const list = pokeDex.pokemon
const inputName = prompt("Enter the name of a pokemon")
const [ pokemonSelected ] = list.filter(pokemon => pokemon.name.toLowerCase() === inputName.toLowerCase())
console.log(pokemonSelected)

I've made a small JSfiddle for you: https://jsfiddle.net/t8uep0ga/10/ Open your console and check the console.log()! 我为您制作了一个小型JSfiddle: https ://jsfiddle.net/t8uep0ga/10/打开您的控制台并检查console.log()! Hope it helps! 希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Chrome中显示JavaScript数组/对象的所有属性? - How can I display all properties of a JavaScript array/object in Chrome? 如何从对象数组渲染所有对象属性 - How to render all object properties from array of objects 如何在Javascript中显示对象数组的所有输入? - How to display all the inputs of an array of objects in Javascript? 定义JavaScript对象文字,以便数组中的所有对象都存在属性? - Define JavaScript object literal so that the properties exists for all objects in an array? 如何从JavaScript中的数组中删除对象的属性? - How to remove properties of objects from a array in javascript? 如何构造一个新的 object 数组,其中包含 JavaScript 中另一个对象数组中的一些已删除属性 - How do I construct a new array of object with some deleted properties from another array of objects in JavaScript 如何从 Ember.js 中的对象数组在页面上显示对象属性? - How to display an object properties on the page from an array of objects in Ember.js? javascript从数组中的所有对象中提取某些属性 - javascript extract certain properties from all objects in array 从对象数组中获取所有唯一对象属性 - Get all unique object properties from array of objects 如何使用 javascript 从对象数组中检索选择性属性并存储为 object? - How to retrieve selective properties from the array of objects and store as an object using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM