React.JS 根据匹配值将映射的 API 响应存储到新数组中
使用 React,我从 API 响应中获取数据。我映射了数据并将作为主标识符的访问 ID 存储到变量中。我想做的是查找任何匹配的访问 ID 值并将它们存储到一个新数组中,我遇到的问题是每个实例都存储在它自己的数组中,例如:
['7065682'] at Index 0
[ '7065682'] 位于索引 1 处
['7065682'] 位于索引 2 处
['7047674'] 位于索引 3 处
['7047674'] 位于索引处4
我想查看每次迭代并检查所有迭代,将匹配值放入它自己的数组中,以便我可以根据每个唯一值编写一些逻辑。我只是不明白如何查看下一次迭代。这是我的代码,以及我一直在尝试的函数“duplicateVisitID”,但它没有给我我正在寻找的结果。
{
Object.keys(this.state.EncounterData).length !== 0 ?
Object.values(this.state.EncounterData).map((encounter, i) => {
const visitID = [encounter.resource.identifier[1].value];
console.log(visitID, i);
const duplicateVisitID = function (visitID) {
if (visitID[i] === visitID[i])
return [visitID.concat(visitID[i])]
}
Using React, I have data from an API response. I mapped the data and am storing the visitID, which would be the main identifier, into a variable. What I would like to do is look for any matching visitID values and store them into a new array, the issue I'm having is each instance is being stored in it's own array, for example:
['7065682'] at Index 0
['7065682'] at Index 1
['7065682'] at Index 2
['7047674'] at Index 3
['7047674'] at Index 4
I would like to look through each iteration and check all, put matching values into it's own array so I can write some logic based off each unique value. I just don't understand how to look through the next iteration. Here's my code, and the function 'duplicateVisitID' that I've been trying, but it doesn't give me the results I'm looking for.
{
Object.keys(this.state.EncounterData).length !== 0 ?
Object.values(this.state.EncounterData).map((encounter, i) => {
const visitID = [encounter.resource.identifier[1].value];
console.log(visitID, i);
const duplicateVisitID = function (visitID) {
if (visitID[i] === visitID[i])
return [visitID.concat(visitID[i])]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你想做什么,但如果我理解正确的话,你想要的新数组只有唯一的字符串,而不是重复的。如果是,请参阅下面的代码。由于迭代内迭代,这不是性能最好的一个,但它会为您工作。您可以稍后通过应用一些算法对其进行优化。
newArr
等于['7065682', '7047674']
I am not sure what do you want to do, but if I understood right you want new array with only strings that are unique, not repeating. If yes see the code below. This is not the best performing one because of iteration inside iteration, but it will work for you. You can optimize it later by applying some algorithms.
The
newArr
is equal to['7065682', '7047674']
如果我理解正确的话,你想要一个唯一值的数组?如果是这样,那么您可以使用映射函数并将任何唯一值添加到数组中(如果该值尚不存在于数组中):
If I understand correctly, you want an array of unique values? If so then you can use the map function and add any unique values to an array if the value is not already in it: