【问题标题】:Render only unique labels in React Native Picker在 React Native Picker 中仅渲染唯一标签
【发布时间】:2018-12-11 16:52:56
【问题描述】:

我有一个 JSON,也可以通过 http://myjson.com/kfd04 访问

{
  "response": [
    {
      "id": "1",
      "customer": "Star Wars",
      "project": "1977"
    },
    {
      "id": "2",
      "customer": "Star Wars",
      "project": "1985"
    },
    {
      "id": "3",
      "customer": "The Matrix",
      "project": "1999"
    },
    {
      "id": "4",
      "customer": "Inception",
      "project": "2010"
    },
    {
      "id": "5",
      "customer": "Interstellar",
      "project": "2014"
    }
  ]
}

我只想呈现唯一的客户这是我到目前为止的代码。它显示所有客户。 responseData 包含对象的“响应”数组。

<Picker
  mode="dialog"
  selectedValue={this.state.customerName}
  onValueChange={(itemValue, itemIndex) => {
    this.setState({ customerName: itemValue });
    }
  }
  >
    {
      this.state.responseData.map((item) => (
      <Picker.Item label={item.customer} value={item.customer} key={item.customer} />))
    }
</Picker>

【问题讨论】:

  • 在渲染前尝试过滤掉值

标签: javascript reactjs react-native ecmascript-6


【解决方案1】:

您需要减少数组以过滤掉唯一值,为此您可以使用 reduce 方法

const data = {
  "response": [
    {
      "id": "1",
      "customer": "Star Wars",
      "project": "1977"
    },
    {
      "id": "2",
      "customer": "Star Wars",
      "project": "1985"
    },
    {
      "id": "3",
      "customer": "The Matrix",
      "project": "1999"
    },
    {
      "id": "4",
      "customer": "Inception",
      "project": "2010"
    },
    {
      "id": "5",
      "customer": "Interstellar",
      "project": "2014"
    }
  ]
}
const arr = data.response.reduce((acc, item) => {
   if(!acc.includes(item.customer)) {
      acc.push(item.customer);
   }
   return acc;
}, [])

console.log(arr);

然后你可以像这样渲染它

render() {

   const reducedArr = this.getUniqueValues();
   return (
         <Picker
            mode="dialog"
            selectedValue={this.state.customerName}
            onValueChange={(itemValue, itemIndex) => {
              this.setState({ customerName: itemValue });
              }
            }
            >
              {
                reducedArr.map((customer) => (
                <Picker.Item label={customer} value={customer} key={customer} />))
              }
            </Picker>
      )
}

const data = {
  "response": [
    {
      "id": "1",
      "customer": "Star Wars",
      "project": "1977"
    },
    {
      "id": "2",
      "customer": "Star Wars",
      "project": "1985"
    },
    {
      "id": "3",
      "customer": "The Matrix",
      "project": "1999"
    },
    {
      "id": "4",
      "customer": "Inception",
      "project": "2010"
    },
    {
      "id": "5",
      "customer": "Interstellar",
      "project": "2014"
    }
  ]
}
const arr = data.response.reduce((acc, item) => {
   if(!acc[item.customer]) {
      acc[item.customer] = 1;
   }
   return acc;
}, {})

console.log(arr);

然后你可以像这样渲染它

render() {

   const reducedObject = this.getUniqueValues();
   return (
         <Picker
            mode="dialog"
            selectedValue={this.state.customerName}
            onValueChange={(itemValue, itemIndex) => {
              this.setState({ customerName: itemValue });
              }
            }
            >
              {
                Object.keys(reducedObject).map((customer) => (
                <Picker.Item label={customer} value={customer} key={customer} />))
              }
            </Picker>
      )
}

或者过滤掉唯一值,你也可以使用Javascript Set

【讨论】:

  • +1 旁注:使用Object 或(如果可用)Set 作为累加器会更有效,因此您不必每次都迭代累加器来检查重复项。
  • @Shubham Khatri:您能否也添加使用 Set 的 sn-p。谢谢!
【解决方案2】:

您可以使用filter 并检查具有特定customer 的电影的第一次出现是否与循环的当前index 具有相同的index。这样你只会得到所有独特的。

const movies = [
  {
    id: "1",
    customer: "Star Wars",
    project: "1977"
  },
  {
    id: "2",
    customer: "Star Wars",
    project: "1985"
  },
  {
    id: "3",
    customer: "The Matrix",
    project: "1999"
  },
  {
    id: "4",
    customer: "Inception",
    project: "2010"
  },
  {
    id: "5",
    customer: "Interstellar",
    project: "2014"
  }
];

const uniqueMovies = movies.filter((movie, index) => {
  return movies.findIndex(m => m.customer === movie.customer) === index;
});

console.log(uniqueMovies);

【讨论】:

    【解决方案3】:

    您可以使用以下函数,如uniqueArray = removeDuplicates(response, 'customer')

    function removeDuplicates(myArr, prop) {
        return myArr.filter((obj, pos, arr) => {
            return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2019-08-14
      • 2015-06-02
      相关资源
      最近更新 更多