简体   繁体   English

我不断收到 [object Object]。 当我尝试从 map 函数获取数据时

[英]I keep getting [object Object]. when I try to get data from the map function

when I put the normal function the map is listed normally, but as soon as I put it under the curly braces and the dollar, I get the sign [object object] .当我放置普通函数时,地图会正常列出,但是一旦我将它放在花括号和美元下, I get the sign [object object] I try to make a drill props my data.我试着做一个钻头道具我的数据。 And i use star wars api (SWAPI)!我使用星球大战 API (SWAPI)!

Thank you for answer!谢谢你的答案!

This is how look like:这是这样的:

This is my code:这是我的代码:

import React from "react";
import Card from "../Card/Card"; 
const CardList = ({ data, valueOfCard, propsdata }) => {
  return (
    <div>
      <div className="card-list">
        {data
          .filter((name) => `${name}.${propsdata}`)
          .slice(0, valueOfCard)
          .map((nameData) => (
            <Card people={`${nameData}.${propsdata}`} /> <-- {nameData.name} work normal
          ))}
      </div>  
      <div></div>
    </div>
  );
};

export default CardList;

The reason you're seeing [object Object] as output text is that the javascript is providing the default string representation of a javasript object.您看到[object Object]作为输出文本的原因是 javascript 提供了 javasript 对象的默认字符串表示。

Since there is a "."因为有“.” just after the [object Object] text, I suspect that nameData is an object instead of something that can actually be interpreted as a string.就在[object Object]文本之后,我怀疑nameData是一个对象,而不是实际上可以解释为字符串的东西。

I am guessing that you're actually looking to use a specific nameData object property like the following:我猜你实际上是想使用一个特定的nameData对象属性,如下所示:

<Card people={`${nameData.someObjectProperty}.${propsdata}`} />

To illustrate what's happening, you can run the following in a javascript debug console:为了说明发生了什么,您可以在 javascript 调试控制台中运行以下命令:

var a = {};
console.log(`${a}`);

I tried to understand your code but they make no sense to me:我试图理解你的代码,但它们对我来说毫无意义:

  • The callback you supplied to Array.prototype.filter returns a string instead of boolean.您提供给Array.prototype.filter的回调返回一个字符串而不是布尔值。
  • The expression `${nameData}.${propsdata}` seems like a strange thing to display.表达式`${nameData}.${propsdata}` 显示起来似乎很奇怪。

You were never clear about what you want your code to do.你从来没有清楚你想要你的代码做什么。 I can only guess.我只能猜测。 I assume:我假设:

  1. You're trying to filter the items in data to get the one that has a property named by the value of propsdata ;您正在尝试过滤data的项目以获取具有由propsdata的值命名的属性的propsdata and
  2. You're expecting `${nameData}.${propsdata}` to show the data of property of nameData named by the value of propsdata .你期待`$ {} nameData $ {propsdata}`显示的属性数据nameData由值命名propsdata

This assumption still doesn't explain why your propsdata apperantly clear empty (if propsdata has "something" in it, the output should be "[object Object].something" instead of "[object Object]." ), but at least the code seem to make some sense.这个假设仍然不能解释为什么你的propsdata显然是空的(如果propsdata"something" ,输出应该是"[object Object].something"而不是"[object Object]." ),但至少是代码似乎有些道理。

If my assumptions were right, then you are seriously misunderstanding how to access javascript object property.如果我的假设是正确的,那么您就严重误解了如何访问 javascript 对象属性。

The backtick: `反引号:`

In javascript, the backtick character is used to declare Template literals .在 javascript 中,反引号字符用于声明模板文字 That is a kind of string that you may put variables in it.这是一种您可以在其中放置变量的字符串。 This lambda function:这个 lambda 函数:

(name) => `${name}.${propsdata}`

is identical to:等同于:

function (name) {
  return "" + name + "." + propsdata;
}

Javascript interpreters would try to turn both name and propsdata into string first, then concat them with ".". Javascript 解释器会尝试propsdata namepropsdata转换为字符串,然后用“.”将它们连接起来。 In your case, name is obviously an object and propsdata seems to be an empty string.在您的情况下, name显然是一个对象,而propsdata似乎是一个空字符串。 Turning an object into string will give you "[object Object]" .把一个对象变成字符串会给你"[object Object]" This template literal will be evaluated into string "[object Object]."此模板文字将被评估为字符串"[object Object]." , which is always true for filtering. ,这对于过滤总是true的。

How to filter by property key?如何按属性键过滤?

To check if an object has a key, you can use Object.prototype.hasOwnProperty .要检查对象是否有键,您可以使用Object.prototype.hasOwnProperty For example:例如:

data.hasOwnProperty("somekey");

Assuming you want only data that has a key dynamically specified by variable propname , you may do a filter like this:假设您只需要具有由变量propname动态指定的键的数据,您可以执行如下过滤器:

data
  .filter((name) => name.hasOwnProperty(propname))

How to access an object property?如何访问对象属性?

If you have an object myobj and you want to access its property named "someprop" , there are 2 types of syntax in javascript:如果您有一个对象myobj并且您想访问其名为"someprop"属性,则 javascript 中有两种类型的语法:

myobj.someprop

and

myobj["someprop"]

The second syntax style allow you to specify a property by the value of another variable.第二种语法风格允许您通过另一个变量的值指定一个属性。 So you can do this:所以你可以这样做:

var myobj = {foo: "bar"};
var keyname = "foo";

console.log(foo[keyname]);
// result in console: bar

How to fix the code?如何修复代码?

If my assumptions are correct, you are probably looking for something like this:如果我的假设是正确的,那么您可能正在寻找这样的东西:

import React from "react";
import Card from "../Card/Card"; 
const CardList = ({ data, valueOfCard, propsdata = "name" }) => {
  return (
    <div>
      <div className="card-list">
        {data
          .filter((nameData) => nameData.hasOwnProperty(propsdata))
          .slice(0, valueOfCard)
          .map((nameData) => (
            <Card people={nameData[propsdata]} />
          ))}
      </div>
      <div></div>
    </div>
  );
};

export default CardList;

Which still doesn't make much sense to me (why show arbitrary property in a card titled "character"?), but it makes more sense than the code you demonstrated.这对我来说仍然没有多大意义(为什么在名为“字符”的卡片中显示任意属性?),但它比您演示的代码更有意义。

If this is not what you wanted to do, my other guess was that you want to filter character by name (and the name is stored in propsdata ).如果这不是您想要做的,我的另一个猜测是您想按名称过滤字符(并且名称存储在propsdata )。 In that case:在这种情况下:

import React from "react";
import Card from "../Card/Card"; 
const CardList = ({ data, valueOfCard, propsdata }) => {
  return (
    <div>
      <div className="card-list">
        {data
          .filter((characterData) => (propsdata === "" || characterData.name === propsdata))
          .slice(0, valueOfCard)
          .map((nameData) => (
            <Card people={characterData.name} />
          ))}
      </div>
      <div></div>
    </div>
  );
};

export default CardList;

The expression (propsdata === "" || characterData.name === propsdata) would allow every character to pass if propsdata is an empty string.如果propsdata是空字符串,表达式(propsdata === "" || characterData.name === propsdata)将允许每个字符通过。

暂无
暂无

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

相关问题 我从 Api 获取数据,但是当我尝试使用 useState 设置值时,它给了我一个空的 object - I get data from Api but when I try to set the value with useState it give me an empty object 尝试从标签获取数据时获取未定义的值 - Getting an undefined value when I try to get data from a label 我不断得到地图不是一个功能 - I keep getting map is not a function 获取未捕获的TypeError:对象# <Object> 我尝试在模板中显示数据时没有方法“获取” - Get Uncaught TypeError: Object #<Object> has no method 'get' when i try to display data in template 当我尝试将数据放入这些 object 时,使用 Vuex 操作获取数据 object 并获得“未捕获(承诺)类型错误” - Fetching a data object with Vuex action and get "Uncaught (in promise) TypeError" when i try to take data into these object 尝试将中间件功能导出到module.exports对象时,为什么会得到“下一个不是功能”的信息? - Why do I get 'Next is not a function' when I try to export a Middleware function to module.exports object? 尝试从 JSONP 访问解析的 JSONP 时,我不断收到 [Object Object] - I keep getting [Object Object] when trying to access parsed JSONP from JSONP 尝试进行申请时,为什么会收到一条消息,指出“对象不是函数”? - Why do I get a message saying “Object is not a function” when I try to do an apply? 尝试创建对象并调用函数时未定义函数 - function not defined when I try to create an object and call function 当我尝试将 Object 从 api 添加到我的 mongodb atlas db NODE.JS 时,我变得不确定 - I get undefined when i try to add an Object from an api to my mongodb atlas db NODE JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM