按名称获取元素字符串

时间:2015-12-01 12:42:01

标签: javascript arrays associative-array

我是JS的新人,所以我需要你的帮助。

我有这个JSON代码:

{
    "data": {
        "people": [

            "get_my_obj": [
                {
                    "yearcome": 2006,
                    "email": "email1@test.com",
                    "came": "1.12",
                    "name": "Alex "
                },
                {
                    "yearcome": 2010,
                    "email": "email2@test.com",
                    "came": "1.12",
                    "name": "John"
                },
                {
                    "yearcome": 2012,
                    "email": "email3@test.com",
                    "came": "1.12",
                    "name": "Max"
                }

            ]
                }
}

我如何通过电子邮件获取元素串? 例如var a = email1@test.com; email2@test.com; email3@test.com;

这对我来说真的很难,因为如果我只有2个对象,那么如果3个对象显示3封电子邮件,则字符串必须显示2封电子邮件。

请帮我找到如何做的方法

1 个答案:

答案 0 :(得分:0)

var s = { data: ....};
var mails = s.data.people.get_my_obj.map(function (e) { return e.email; }).join(';');
console.log(mails);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map