无法在js中连接Object中的值

时间:2015-03-10 18:07:03

标签: javascript

我知道这很傻,但是:

window.temp1.targetInterests

返回我:

Object {Famosos: "Famosos", Musica: "Música", Humor: "Humor"}

我尝试join

window.temp1.targetInterests.join('/')

map

window.temp1.targetInterests.map(function(elem){
    return elem.name;
}).join(",");

但它一直让我回来

 Uncaught TypeError: undefined is not a function

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用Object.keys将密钥加在一起:

var joinedKeys = Object.keys(window.temp1.targetInterests).join(",");

如果您想要实际值,则必须迭代对象:

var joinedValues = [];
for (var key in window.temp1.targetInterests) {
    joinedValues.push(window.temp1.targetInterests[key]);
}
joinedValues = joinedValues.join(",");