如何在值是数组的对象数组中映射? Javascript?

时间:2018-11-07 16:38:54

标签: javascript arrays

我正在尝试对数组进行map,但是它不起作用,它是对象的数组,但是对象的值是数组。.

示例:

   itemsToFilter = [{
    "domain_name": [
      "Sales and Marketing Systems",
      "Finance and Supply Chain",
      "Global Infrastructure & CIO Services",
      "Systems and Tools",
      "Quote to Cash & Services",
      "Network Engineering",
      "CFO",
      "Cloud, Collaboration, Support & Identity",
      "Digital Workplace Engineering",
      "CIO Design",
      "Confidential",
      "CIO Domain"
    ]
  },
  {
    "subdomain_name": [
      "Territory, Quota and Coverage",
      "Analytics@IBM",
      "CIO Business Development",
      "HW/SW Manufacturing and Plan to Supply",
      "Virtualisation and Data Center Platforms",
      "HR Talent & Travel IT",
      "Quote to Cash and Services",
      "Finance",
      "CIO Services Japan",
      "Global Business Partners",
      "Global Financing",
      "Client Support IT",
      "Automation & Tooling",
      "Toolbox@IBM",
    ]
  }
}

我正在这样做:

itemsToFilter.map(i => i.domain_name)

我希望所有值都仅为domain_name

但是结果是:

enter image description here

1 个答案:

答案 0 :(得分:2)

线索在您的变量名中:要过滤器的项目。

您要过滤数组,而不是映射数组中的每个项目到另一个值。

itemsToFilter = [{
    "domain_name": [
      "Sales and Marketing Systems",
      "Finance and Supply Chain",
      "Global Infrastructure & CIO Services",
      "Systems and Tools",
      "Quote to Cash & Services",
      "Network Engineering",
      "CFO",
      "Cloud, Collaboration, Support & Identity",
      "Digital Workplace Engineering",
      "CIO Design",
      "Confidential",
      "CIO Domain"
    ]
  },
  {
    "subdomain_name": [
      "Territory, Quota and Coverage",
      "Analytics@IBM",
      "CIO Business Development",
      "HW/SW Manufacturing and Plan to Supply",
      "Virtualisation and Data Center Platforms",
      "HR Talent & Travel IT",
      "Quote to Cash and Services",
      "Finance",
      "CIO Services Japan",
      "Global Business Partners",
      "Global Financing",
      "Client Support IT",
      "Automation & Tooling",
      "Toolbox@IBM",
    ]
  }
];

const filteredItems = itemsToFilter.filter(item => "domain_name" in item);

console.log(filteredItems);