英文:
Get data from array with object
问题
我正在尝试访问数组并获取值 "estado"
。我正在使用 vuejs
和 laravel
。我尝试了各种方法,但是无法成功... 我知道我的问题可能很简单...
我的数组是:
[{"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: '[{"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}]'}}; // Mocking your data
const data = JSON.parse(scope.row.estadosFicheros)
console.log(data[0].estado);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论