简体   繁体   English

如何在 console.log 上将数组名称从 json 编码为 javascript?

[英]How to get array name from json encode to javascript on console.log?

I have an array.我有一个数组。 I converted php array inside JavaScript. I want to get array's name for my select option.我在 JavaScript 中转换了 php 数组。我想为我的 select 选项获取数组名称。 How to get array name on console log?如何在控制台日志中获取数组名称?

My code is below;我的代码如下;

<script> 
var users= <?php echo json_encode($users) ?>;
        console.log(users);
</script>

[{0: {id:1, name: Alex, email: alex@gmail.com}}, [{0: {id:1, 姓名: Alex, email: alex@gmail.com}},

{1: {id:2, name: Jane, email: jane@gmail.com}}] {1:{id:2,姓名:简,email:jane@gmail.com}}]

This is what I get.这就是我得到的。

But this is what I want.但这就是我想要的。

Alex亚历克斯

Jane

Can you help me please?你能帮我吗?

You got use flatMap()你使用了 flatMap()

 const users = [{0: {id:1, name: "Alex", email: "alex@gmail.com"}},{1: {id:2, name: "Jane", email: "jane@gmail.com"}}] const parsedUsers = users.flatMap(user=>Object.values(user)); parsedUsers.forEach(user=>console.log(user.name));

you can use @json blade directive:你可以使用@json刀片指令:

<script> 
var users= @json($users);
        console.log(users);
</script>

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

相关问题 当console.log时,来自JSON响应的名称变得不确定 - Name from JSON response getting undefined when console.log console.log是否格式化Json对象有名称吗? - Is there a name for how console.log formats the Json object? 如何从仅出现在console.log()中而不出现在JSON.stringify()中的JavaScript对象中获取属性? - How to get property out of JavaScript object which is only appearing in console.log() but not in JSON.stringify()? console.log(data)到javascript中的json文件 - console.log(data) to json file in javascript 如何在function中获取console.log的JSON数据并应用? - How to get JSON data of console.log inside function and apply it? 如何使用 JavaScript 获得要在 console.log() 中显示的积分总和? - How to get the sum of credits to show in console.log() using JavaScript? Javascript + Firebug console.log(),如何获取异常? - Javascript + Firebug console.log(), how to not get exceptions? 如何在javascript代码中获取console.log的结果字符串? - How to get result string of console.log in javascript code? 如何在 JavaScript 中获取 console.log 内容作为字符串 - How to get the console.log content as string in JavaScript 如何使用javascript console.log在ssh中获得相等的间距 - How to get equal spacing in ssh with javascript console.log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM