使用Undescore从对象属性创建一个大数组

时间:2016-08-29 06:44:33

标签: javascript underscore.js

我有以下结构:

Object = {
1:[..],
2:[..],
3:[..],
4:[..]
}

每个属性只是一个字符串数组。

我想知道,如何使用下划线创建一个包含所有独特元素的大数组。

谢谢!

1 个答案:

答案 0 :(得分:3)

假设这个对象:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Items"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:include schemaLocation="BaseTypes.xsd" />

  <xs:element name="Items" nillable="true" type="ArrayOfItem" />

  <xs:complexType name="ArrayOfItem">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="Item" nillable="true" type="Item" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="Item">
    <xs:all>
      <xs:element minOccurs="0" maxOccurs="1" name="ValidationErrors" type="ArrayOfValidationError" />
      <xs:element minOccurs="0" maxOccurs="1" name="Warnings" type="ArrayOfWarning" />

      <xs:element name="ItemID" type="uniqueIdentifier" minOccurs="0" maxOccurs="1" />
      <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="PurchaseDescription" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="Code" type="xs:string" minOccurs="0" maxOccurs="1" />

      <xs:element name="UpdatedDateUTC" minOccurs="0" maxOccurs="1" type="xs:dateTime"/>
      <xs:element name="SalesDetails" type="ItemPriceDetails" minOccurs="1" maxOccurs="1" />
      <xs:element name="PurchaseDetails" type="ItemPriceDetails" minOccurs="1" maxOccurs="1" />

      <xs:element name="Name" type="itemNameType" minOccurs="0" maxOccurs="1" />
      <xs:element name="IsTrackedAsInventory" type="xs:boolean" minOccurs="0" maxOccurs="1" nillable="false" />
      <xs:element name="InventoryAssetAccountCode" type="accountCode" minOccurs="0" maxOccurs="1" />
      <xs:element name="TotalCostPool" type="xs:decimal" minOccurs="0" maxOccurs="1" />
      <xs:element name="QuantityOnHand" type="xs:decimal" minOccurs="0" maxOccurs="1" />
      <xs:element name="IsSold" type="xs:boolean" minOccurs="0" maxOccurs="1" nillable="false" />
      <xs:element name="IsPurchased" type="xs:boolean" minOccurs="0" maxOccurs="1" nillable="false" />

    </xs:all>
    <xs:attribute name="status" type="entityValidationStatus" use="optional" />
  </xs:complexType>

  <xs:complexType name="ItemPriceDetails">
    <xs:all>
      <xs:element minOccurs="0" maxOccurs="1" name="ValidationErrors" type="ArrayOfValidationError" />
      <xs:element minOccurs="0" maxOccurs="1" name="Warnings" type="ArrayOfWarning" />

      <xs:element name="UnitPrice" type="itemPrice" minOccurs="0" maxOccurs="1" />
      <xs:element name="TaxType" type="taxType" minOccurs="0" maxOccurs="1" />
      <xs:element name="AccountCode" type="accountCode" minOccurs="0" maxOccurs="1" />
      <xs:element name="COGSAccountCode" type="accountCode" minOccurs="0" maxOccurs="1" />

    </xs:all>
    <xs:attribute name="status" type="entityValidationStatus" use="optional" />
  </xs:complexType>

</xs:schema>

您可以展平所有对象的值:

var o = {
    1:['3','4'],
    2:['5','6'],
    3:['7','8'],
    4:['9','10']
}

获取阵列。

编辑: 我使用数字,问题是关于字符串。它适用于任何类型的数据。

要删除重复项,您可以使用适用于字符串和数字的_.uniq