在javascript中只获取对象的第一个对象

时间:2015-10-20 16:34:23

标签: javascript object

你好我在JavaScript中有一个包含这个的对象:

{Coc Coc={30daysAgo=1}, Mozilla={30daysAgo=1}, BlackBerry={30daysAgo=1}, Safari={30daysAgo=140}, Firefox={30daysAgo=192}, YaBrowser={30daysAgo=6}, Internet Explorer={30daysAgo=72}, Safari (in-app)={30daysAgo=40}, Android Browser={30daysAgo=3}, Chrome={30daysAgo=1387}, SeaMonkey={30daysAgo=1}, Edge={30daysAgo=7}, Opera={30daysAgo=7}}

如何在变量中只插入没有其余浏览器的浏览器名称?

例如var browsers = Mozilla,BlackBerry,Safari等...

1 个答案:

答案 0 :(得分:1)

试试这个:

var a = {"Coc Coc":{"30daysAgo":1}, Mozilla:{"30daysAgo":1}, BlackBerry:{"30daysAgo":1}, Safari:{"30daysAgo":140}, Firefox:{"30daysAgo":192}, YaBrowser:{"30daysAgo":6}, "Internet Explorer":{"30daysAgo":72}, "Safari (in-app)":{"30daysAgo":40}, "Android Browser":{"30daysAgo":3}, Chrome:{"30daysAgo":1387}, SeaMonkey:{"30daysAgo":1}, Edge:{"30daysAgo":7}, Opera:{"30daysAgo":7}};

var browsers = Object.keys(a).join(",");

结果:

  

Coc Coc,Mozilla,BlackBerry,Safari,Firefox,YaBrowser,Internet Explorer,Safari(应用内),Android浏览器,Chrome,SeaMonkey,Edge,Opera

Fiddle