[英]JavaScript find() method searches through an array of objects to find an element, the outcome not accessible
//array of objects
const posts = [ {userId: 1, id: 1, title: "sunt", author:
"Tom"},
{userId: 1, id: 2, title: "qui est esse",author: "Tom"},
{userId: 1, id: 3, title: "ea molestias", author: "Tom"},
{userId: 1, id: 4, title: "eum et est occaecati", author:
"Tom"}]
//id of one of the object
const ItemId = 2
//trying to find the object that has id: 2 an then print other
key -values pairs from that prticular object
let post= posts.find(item=> item.id===ItemId)
console.log(post)
//result: {userId: 1, id: 2, title: "qui est esse",author:
"Tom"}
console.log(post.title)
//result: undefined --- why can not access these properties?
console.log(post.author)
//result: undefined
console.log (typeof post)
//result: "string" --- why string, its an object is it?
Appreciate any insights, I dont follow why those find() method results can not be access by the way we access propreties in objects?感谢任何见解,我不明白为什么那些 find() 方法的结果不能通过我们访问对象中的属性的方式来访问? I have also tried using brackets like this: post[author) with no success... Thanks a lot for anything on the subject
我也尝试过使用这样的括号: post[author) 没有成功......非常感谢关于这个主题的任何事情
Don't you get any errors?你没有收到任何错误吗? because when I copied your code and run then it has multiple errors.
因为当我复制您的代码并运行时,它有多个错误。
//array of objects
const posts = [ {userId: 1, id: 1, title: "sunt", author: "Tom"},
{userId: 1, id: 2, title: "qui est esse",author: "Tom"},
{userId: 1, id: 3, title: "ea molestias", author: "Tom"},
{userId: 1, id: 4, title: "eum et est occaecati", author:
"Tom"}];
let post= posts.find(item=> item.id===2)
console.log(post)
console.log(post.title)
console.log(post.author)
console.log (typeof post)
Try this code.试试这个代码。 Its is returning everything correct
它正在返回正确的一切
Your code seem to have a couple of syntax errors:您的代码似乎有几个语法错误:
First you forgot to put a comma after one of the elements in the array.首先,您忘记在数组中的一个元素之后放置逗号。 Each element should be separated by a comma
{userId: 1, id: 2, title: "qui est esse",author: "Tom"},
每个元素应该用逗号分隔
{userId: 1, id: 2, title: "qui est esse",author: "Tom"},
A local variable in Javascript can be set a value using a =
sign. Javascript 中的局部变量可以使用
=
符号设置值。 In your code you were using const ItemId: 2
.在您的代码中,您使用的是
const ItemId: 2
。 Declaring a variable using :
should only be used when declaring it inside an Object, but not when declaring a local variable or constant.使用
:
声明变量仅应在 Object 中声明时使用,但在声明局部变量或常量时不应使用。
This is the full code with the fixed syntactical errors:这是具有固定语法错误的完整代码:
//array of objects
const posts = [ {userId: 1, id: 1, title: "sunt", author:
"Tom"},
{userId: 1, id: 2, title: "qui est esse",author: "Tom"},
{userId: 1, id: 3, title: "ea molestias", author: "Tom"},
{userId: 1, id: 4, title: "eum et est occaecati", author:
"Tom"}];
//id of one of the object
const ItemId = 2;
//trying to find the object that has id: 2 an then print other key -values pairs from that prticular object
let post= posts.find(item=> item.id===ItemId)
console.log(post)
//result: {userId: 1, id: 2, title: "qui est esse",author: "Tom"}
console.log(post.title)
//result: undefined --- why can not access these properties?
console.log(post.author)
//result: undefined
console.log (typeof post)
//result: "string" --- why string, its an object is it?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.