从数组中获取对象的数据

huangapple go评论95阅读模式
英文:

Get data from array with object

问题

我正在尝试访问数组并获取值 "estado"。我正在使用 vuejslaravel。我尝试了各种方法,但是无法成功... 我知道我的问题可能很简单...

我的数组是:

[{"tarea_id":"15359","estado":2,"fechaHora":"2023-06-20 13:16:30.787","name":null},{"tarea_id":"15359","estado":0,"fechaHora":"2023-06-20 13:16:29.787","name":null}]

我用于在网页中显示数据的代码是:

{{ scope.row.estadosFicheros }}

如果我尝试使用:scope.row.estadosFicheros[0] 显示 [,如果我尝试在 scope.row.estadosFicheros.estado 中获取信息,返回 undefined,如果我执行 JSON.parse(scope.row.estadosFicheros),返回我一开始写的内容。如果我在解析后执行 .estado,返回 undefined,如果我尝试使用 ["estado"] 也会遇到相同的问题。

我尝试使用 strigify 可以显示这个:

[{"tarea_id":"15365","estado":1,"fechaHora":"2023-06-20 13:35:55.227","name":null},{"tarea_id":"15365","estado":0,"fechaHora":"2023-06-20 13:35:35.117","name":null}]

如果我尝试用 .estado 获取 estado,什么都不显示。

我做错了什么?我如何才能访问这个带有对象的数组?

谢谢阅读并对我的糟糕英语表示抱歉。

英文:

I´m trying to access to array and get value "estado". I´m working with vuejs and laravel. I´m trying with any way, but i can´t... I know that my question maybe it´s very easy or simple...

My array it´s:

[{"tarea_id":"15359","estado":2,"fechaHora":"2023-06-20 13:16:30.787","name":null},{"tarea_id":"15359","estado":0,"fechaHora":"2023-06-20 13:16:29.787","name":null}]

My code to show data en webrowser it´s:

{{ scope.row.estadosFicheros }}

If i try with: scope.row.estadosFicheros[0] show [ if i try to ge info in scope.row.estadosFicheros.estado return undefined if i do JSON.parse(scope.row.estadosFicheros) return what i wrote at the beginning
if i do after parse .estado return undefined if i do ["estado"] same problem

I tryed with strigify i can show this: [{\"tarea_id\":\"15365\",\"estado\":1,\"fechaHora\":\"2023-06-20 13:35:55.227\",\"name\":null},{\"tarea_id\":\"15365\",\"estado\":0,\"fechaHora\":\"2023-06-20 13:35:35.117\",\"name\":null}]"

if i try to get estado with .estado not show anything.

What i´m doing wrong? how i can to access this array with object?

Thanks for readme and sorry for my bad english

答案1

得分: 2

你的问题是在访问数据之前需要解析数据。

你需要首先解析你的数据。使用 JSON.parse(scope.row.estadosFicheros) 这将给你一个对象。之后你可以访问你的对象,例如 JSON.parse(scope.row.estadosFicheros)[0].estado

我已经创建了一个示例,以更好地理解你遇到的问题。

const scope = {row: { estadosFicheros: '[{"tarea_id":"15359","estado":2,"fechaHora":"2023-06-20 13:16:30.787","name":null},{"tarea_id":"15359","estado":0,"fechaHora":"2023-06-20 13:16:29.787","name":null}]'}}; // 模拟你的数据
const data = JSON.parse(scope.row.estadosFicheros);
console.log(data[0].estado);
英文:

Your problem is that you need to parse your data before you access it.

You need to parse your data first. JSON.parse(scope.row.estadosFicheros) this will give you an object. After that you can access your object eg. JSON.parse(scope.row.estadosFicheros)[0].estado

I have created an example of how your data probably looks to better understand the problem you are encountering.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const scope = {row: { estadosFicheros: &#39;[{&quot;tarea_id&quot;:&quot;15359&quot;,&quot;estado&quot;:2,&quot;fechaHora&quot;:&quot;2023-06-20 13:16:30.787&quot;,&quot;name&quot;:null},{&quot;tarea_id&quot;:&quot;15359&quot;,&quot;estado&quot;:0,&quot;fechaHora&quot;:&quot;2023-06-20 13:16:29.787&quot;,&quot;name&quot;:null}]&#39;}}; // Mocking your data
const data = JSON.parse(scope.row.estadosFicheros)
console.log(data[0].estado);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 18:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定