如何把对象转换成数组

       Lodash是一个著名的javascript原生库,不需要引入其他第三方依赖。是一个意在提高开发者效率,提高JS原生方法性能的JS库。文档可通过官网下载,也可点此链接下载https://pan.baidu.com/s/1dFNYB8P(被吞了可以留言联系我)

      在lodash中有两种对象转换成数组的方法,一种是使对象属性值转换成数组,另一种是使对象的属性名转换成数组。

一:_.values(使对象属性值转换成数组)

_.values(object)

(Array): 返回对象属性的值的数组。

var items={"可口可乐":{"classification":"饮料","name":"可口可乐","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},"雪碧":{"classification":"饮料","name":"雪碧","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},"苹果":{"classification":"水果","name":"苹果","unit":"斤","price":"5.5","count":3,"subtotal":"16.5元","free_count":1,"free_money":3},"荔枝":{"classification":"水果","name":"荔枝","unit":"斤","price":"2","count":3,"subtotal":"6元","free_count":1,"free_money":3},"电池":{"classification":"生活用品","name":"电池","unit":"个","price":"15","count":2,"subtotal":"30元","free_count":1,"free_money":3},"方便面":{"classification":"食品","name":"方便面","unit":"袋","price":"4.5","count":2,"subtotal":"9元","free_count":1,"free_money":3}}
_.values(items)//[{"classification":"饮料","name":"可口可乐","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},{"classification":"饮料","name":"雪碧","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},{"classification":"水果","name":"苹果","unit":"斤","price":"5.5","count":3,"subtotal":"16.5元","free_count":1,"free_money":3},{"classification":"水果","name":"荔枝","unit":"斤","price":"2","count":3,"subtotal":"6元","free_count":1,"free_money":3},{"classification":"生活用品","name":"电池","unit":"个","price":"15","count":2,"subtotal":"30元","free_count":1,"free_money":3},{"classification":"食品","name":"方便面","unit":"袋","price":"4.5","count":2,"subtotal":"9元","free_count":1,"free_money":3}]

二:_.keys(使对象的属性名转换成数组)

 _.keys(object)

返回(Array): 返回包含属性名的数组。

var items={"可口可乐":{"classification":"饮料","name":"可口可乐","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},"雪碧":{"classification":"饮料","name":"雪碧","unit":"瓶","price":"3","count":3,"subtotal":"6元(原价:    9元)","free_count":1,"free_money":3},"苹果":{"classification":"水果","name":"苹果","unit":"斤","price":"5.5","count":3,"subtotal":"16.5元","free_count":1,"free_money":3},"荔枝":{"classification":"水果","name":"荔枝","unit":"斤","price":"2","count":3,"subtotal":"6元","free_count":1,"free_money":3},"电池":{"classification":"生活用品","name":"电池","unit":"个","price":"15","count":2,"subtotal":"30元","free_count":1,"free_money":3},"方便面":{"classification":"食品","name":"方便面","unit":"袋","price":"4.5","count":2,"subtotal":"9元","free_count":1,"free_money":3}}
_.keys(items)//["可口可乐","雪碧","苹果","荔枝","电池","方便面"]

猜你喜欢

转载自1150404757.iteye.com/blog/2322806
今日推荐