【问题标题】:Need Help Accessing ID Property In Complex Array Object需要帮助访问复杂数组对象中的 ID 属性
【发布时间】:2019-04-11 03:04:34
【问题描述】:

在尝试通过我从 this.match.params 对象获取的 ID 过滤此数组时遇到问题。我正在尝试将其与数组中的 ID 匹配以供审核,但我一直不确定。

我尝试使用按 ID 过滤,但由于可能没有过滤正确的对象,因此返回空匹配

const id = this.props.match.params === cju980jb901l00740i07skgpc 所以这个 id 是我试图过滤的那个,但我似乎无法使用 array.filter((review => review.id === id)

0:
Author: {id: "cju9808n401fo0740rbqgej9p", username: "asdfsd", 
email: "asldkf@gmail.com", __typename: "User"}
ProjectReviewed: {id: "cju980j9a01kd07407qvtwh5o", name: "Amy's 1st 
Project", titleImg: "http://lorempixel.com/400/400/", __typename: 
"Project"}
id: "cju980jb901l00740i07skgpc"
name: "Nice Job!"
text: "I really like how you did this."
timestamp: "2019-04-05T00:00:00.000Z"
__typename: "Review"
__proto__: Object
1: {id: "cju980jcz01l80740jwohw41e", name: "Wicked", text: "You're 
a rockstar!", timestamp: "2019-04-01T00:00:00.000Z", Author: {…}, 
…}
2: {id: "cju980jdh01lg0740j8o4u60o", name: "Well Done", text: "I'm 
amazed - truly.", timestamp: "2019-03-19T00:00:00.000Z", Author: 
{…}, …}
3: {id: "cju980jdy01lo0740mmo1yfkf", name: "DiS iS TrASSh", text: 
"Thank u, next!", timestamp: "2019-01-12T00:00:00.000Z", Author: 
{…}, …}
4: {id: "cju980jev01lw0740p6k8r7d3", name: "Teach Me How to Do 
This!", text: "Notice me senpai", timestamp: "2019-02- 
19T00:00:00.000Z", Author: {…}, …}
5: {id: "cju980jfs01m40740pe5gxn9a", name: "This is nice", text: 
"...real nice", timestamp: "2019-01-11T00:00:00.000Z", Author: {…}, 
 …}
6: {id: "cju980jge01mc0740mqyu5zi3", name: "Noob", text: "Why are 
you even posting a tutorial about something you know nothing 
about?", timestamp: "2018-07-14T00:00:00.000Z", Author: {…}, …}
7: {id: "cju980jgx01mk0740g1ehxjsf", name: "Wow", text: "You gotta 
create more of these.", timestamp: "2019-04-01T00:00:00.000Z", 
Author: {…}, …}
8: {id: "cju980jb901l00740i07skgpc", name: "Nice Job!", text: "I 
really like how you did this.", timestamp: "2019-04- 
05T00:00:00.000Z", Author: {…}, …}
9: {id: "cju980jcz01l80740jwohw41e", name: "Wicked", text: "You're 
a rockstar!", timestamp: "2019-04-01T00:00:00.000Z", Author: {…}, 
…}
10: {id: "cju980jdh01lg0740j8o4u60o", name: "Well Done", text: "I'm 
amazed - truly.", timestamp: "2019-03-19T00:00:00.000Z", Author: 
{…}, …}
11: {id: "cju980jdy01lo0740mmo1yfkf", name: "DiS iS TrASSh", text: 
"Thank u, next!", timestamp: "2019-01-12T00:00:00.000Z", Author: 
{…}, …}
12: {id: "cju980jev01lw0740p6k8r7d3", name: "Teach Me How to Do 
This!", text: "Notice me senpai", timestamp: "2019-02- 
19T00:00:00.000Z", Author: {…}, …}
13: {id: "cju980jfs01m40740pe5gxn9a", name: "This is nice", text: 
"...real nice", timestamp: "2019-01-11T00:00:00.000Z", Author: {…}, 
…}
14: {id: "cju980jge01mc0740mqyu5zi3", name: "Noob", text: "Why are 
you even posting a tutorial about something you know nothing 
about?", timestamp: "2018-07-14T00:00:00.000Z", Author: {…}, …}
15: {id: "cju980jgx01mk0740g1ehxjsf", name: "Wow", text: "You gotta 
create more of these.", timestamp: "2019-04-01T00:00:00.000Z", 
Author: {…}, …}

【问题讨论】:

  • 请不要从控制台复制粘贴。提供简短而真实的数据(不是来自控制台);
  • 这是在 React 中的状态上,我将这个数组设置为状态,我们正在使用 GraphQL。不确定您希望我发布什么,请详细说明您的任务,
  • 我的意思是你的数据不可读。请添加我们可以测试和轻松阅读的数据。您可以在数据上使用JSON.stringify() 并粘贴到此处。

标签: javascript arrays reactjs filtering


【解决方案1】:

这将被id过滤掉:

const arr = [{
    Author: {
      id: "cju9808n401fo0740rbqgej9p",
      username: "asdfsd",
      email: "asldkf@gmail.com",
      __typename: "User"
    },
    ProjectReviewed: {
      id: "cju980j9a01kd07407qvtwh5o",
      name: "Amy's 1st ,Project ",
      titleImg: ",http: //lorempixel.com/400/400/",
      __typename: "Project"
    },
    id: "cju980jb901l00740i07skgpc",
    name: "Nice Job!",
    text: "I really like how you did this.",
    timestamp: "2019-04-05T00:00:00.000Z"
  },
  {
    Author: {
      id: "cju9808n401fo0740rbqgej9p",
      username: "asdfsd",
      email: "asldkf@gmail.com",
      __typename: "User"
    },
    ProjectReviewed: {
      id: "cju980j9a01kd07407qvtwh5o",
      name: "Amy's 1st ,Project ",
      titleImg: ",http: //lorempixel.com/400/400/",
      __typename: "Project"
    },
    id: "1cju980jb901l00740i07skgpc",
    name: "Nice Job!",
    text: "I really like how you did this.",
    timestamp: "2019-04-05T00:00:00.000Z"
  }
];
const matchId = 'cju980jb901l00740i07skgpc';
let res = arr.filter(({
  id
}) => id === matchId);
console.log(res);

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2021-05-07
    相关资源
    最近更新 更多