如何在JavaScript中获取内部数组

时间:2019-03-24 15:30:29

标签: javascript arrays

我正在尝试从数组中获取所有内容。 这是提取数据以通过innerHTML显示的功能:

window.location.href = 'gonative://contacts/getAll?callback=contacts_callback';

function contacts_callback(obj) {
    var contactinfo = obj.contacts.map(({givenName}) => givenName) + " " + 
                      obj.contacts.map(({familyName}) => familyName) + " " + " (" + 
                      obj.contacts.map(({organizationName}) => organizationName) + ") " + 
                      obj.contacts.map(({phoneNumbers.phoneNumber}) => phoneNumbers.phoneNumber) + "<br>";
    document.getElementById("demo").innerHTML = contactinfo;
}

这是一个只有2个联系人时输入内容的示例:

{"success":true,"contacts":[
        {
            "emailAddresses":[],
            "phoneNumbers":
                [
                    {
                        "label":"unknown",
                        "phoneNumber":"XXX-XXXXXXX"
                    }
                ],
            "givenName":"John",
            "organizationName":"Apple",
            "familyName":"Appleseed",
        },
        {
            "emailAddresses":[],
            "phoneNumbers":
                [
                    {
                        "label":"unknown",
                        "phoneNumber":"XXX-XXXXXXX"
                    }
                ],
            "givenName":"John",
            "organizationName":"Apple",
            "familyName":"Appleseed",
        },
    ]
}

我只希望结果列为:

  

John Appleseed(苹果)XXX-XXXXXXX
  约翰·Appleseed(苹果)XXX-XXXXXXX

3 个答案:

答案 0 :(得分:1)

两个问题:

  • 您正在显示所有给定名称,然后显示所有姓氏,等等,每个都有单独的.map()调用。而是只对数组执行一次.map()调用,然后显示每个迭代对象的属性。
  • phoneNumbers.phoneNumber不是正确的参考。 phoneNumbers是一个数组,因此您应该对其进行迭代。

也:

  • 模板文字使构建字符串可能更容易
  • 您可以使用.join("<br>")将行和换行符粘合在一起。

这是更正的版本:

function contacts_callback(obj) {
    var contactinfo = obj.contacts.map(o => 
        `${o.givenName} ${o.familyName} (${o.organizationName}) ${
             o.phoneNumbers.map(n => n.phoneNumber)
        }`)
        .join("<br>");
    document.getElementById("demo").innerHTML = contactinfo;
}

// Demo
var obj = {"success":true,"contacts":[{"emailAddresses":[],"phoneNumbers":[{"label":"unknown","phoneNumber":"XXX-XXXXXXX"}],"givenName":"John","organizationName":"Apple","familyName":"Appleseed",},{"emailAddresses":[],"phoneNumbers":[{"label":"unknown","phoneNumber":"XXX-XXXXXXX"}],"givenName":"John","organizationName":"Apple","familyName":"Appleseed",},]};

contacts_callback(obj);
<div id="demo"></div>

答案 1 :(得分:0)

很难给出答案,因为很难告诉您phoneNumbers数组中可以包含的内容,以及是否还要为该数组中的每个电话号码显示一行。

我会做这样的事情:

function contacts_callback(obj) {
  let arrayContacts = [];

  // Iterate over all your contacts
  obj.contacts.forEach(item => {
    // Iterate over each contact's phone numbers
    item.phoneNumbers.forEach(phone => {
      // Building your string with string interpolation and pushing to result array
      // You could also add <br> or other tags needed here
      arrayContacts.push(`${item.givenName} ${item.familyName} (${item.organizationName}) ${phone.phoneNumber}`);
    });    
  });
  // Return your array, use it in your innerHTNL, etc.
  return arrayContacts;
} 

答案 2 :(得分:0)

如果您的obj被称为“ obj”,则为:

const result = obj.contacts.map(contact =>{
    return `${contact.givenName} ${contact.familyName} (${contact.organizationName}) ${contact.phoneNumbers[0].phoneNumber}`
}

此代码将返回U询问的一系列信息,但是如果用户拥有多个电话号码,则它将仅从列表中获取第一位