[英]How can i use $.map with Object.keys function to get the first value from array given for Object.keys?
I have this json array: 我有这个json数组:
//val
0: {name: "jeg", class: "klase-jeg", id: "id-jeg"} 1: {age: 11, id: "jeg-id-11"} 2: {mail: "jeg mail", class: "jeg-klas-mail"}
and i want to get the first key of each json: 我想获取每个json的第一个键:
(name,age,mail)
This is my function: 这是我的功能:
$.map(val, Object.keys) //Result: ["name", "class", "id", "age", "id", "mail", "class"]
But the result is all the keys. 但是结果是所有的关键。
to get the first key from a json i used this: 要从json获取第一个密钥,我使用了以下命令:
obj = val[0]; Object.keys(obj)[0]); //Result: name
and i tried to use it with map but it didnt work :( 我试图将其与地图一起使用,但没有成功:(
$.map(val, Object.keys[0]) //failed
i can use each or for, but i would like to know if can i use map with object.key to get the first key of each json. 我可以使用每个或为,但我想知道是否可以将map与object.key一起使用以获取每个json的第一个键。
Thanks! 谢谢!
I think you could do this : 我认为您可以这样做:
assuming : 假设 :
a = [{name: "jeg", class: "klase-jeg", id: "id-jeg"},{age: 11, id: "jeg-id-11"},{mail: "jeg mail", class: "jeg-klas-mail"}]
then do : 然后做 :
a.map((el)=>{
return Object.keys(el)[0];
});
Hope it helps ! 希望能帮助到你 !
You can use map
and Object.keys
like this: 您可以像这样使用
map
和Object.keys
:
const val = [ {name: "jeg", class: "klase-jeg", id: "id-jeg"}, {age: 11, id: "jeg-id-11"}, {mail: "jeg mail", class: "jeg-klas-mail"}] const firstKeys = val.map(a => Object.keys(a)[0]); console.log(firstKeys) /* In jquery: $.map(val, a => Object.keys(a)[0]) */
$.map(val, Object.keys[0])
will throw an error because Object.keys
is a function
, not an array. $.map(val, Object.keys[0])
将引发错误,因为Object.keys
是一个function
,而不是数组。
Update: Please note that properties' order in objects is not guaranteed in javaScript 更新:请注意, javaScript中不能保证对象中属性的顺序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.