按键获取对象,然后从其他键同一对象获取值

时间:2017-02-02 20:17:22

标签: javascript reactjs

我有一个对象数组

"fields": [
    {
      "label": "Country",
      "name": "country",
      "picklistValues": ["USA", "CAN"],
      "type": "string"
    },
    {
      "label": "Day",
      "name": "day",
      "picklistValues": ["Monday", "Tuesday", "Wednesday"],
      "type": "string"
    },
    {...},
  ]

使用此代码

  renderTerritoryMeta() {
      return this.props.territoryMeta.map((territoryField) => {
        return (
          <div className="slds-grid slds-box slds-box--x-small tile">
              {territoryField.label}

          </div>
        );
      });
  }

enter image description here

它渲染数组中的所有对象。我希望按键name加上值获取特定对象,然后获取同一对象的picklistValues

// with this array
// get the object by key 'name' & value 'country'
// then with same object get values from key 'pickListValues'

2 个答案:

答案 0 :(得分:1)

您可以在div渲染之前使用条件 我会尝试这样的事情:

renderTerritoryMeta() {
      return this.props.territoryMeta.map((territoryField) => {
        const shouldRender = territoryField.name === 'country'
        let pickListValues = []
        if (shouldRender) pickListValues = territoryField.pickListValues
        if (!shouldRender) return null
        return (
          <div className="slds-grid slds-box slds-box--x-small tile">
              {territoryField.label}

          </div>
        );
      });
  }

答案 1 :(得分:0)

当name = country时短路地图。

<abbr class="timeago">
相关问题