React.JS 根据匹配值将映射的 API 响应存储到新数组中

发布于 2025-01-11 03:18:53 字数 848 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2025-01-18 03:18:53

我不确定你想做什么,但如果我理解正确的话,你想要的新数组只有唯一的字符串,而不是重复的。如果是,请参阅下面的代码。由于迭代内迭代,这不是性能最好的一个,但它会为您工作。您可以稍后通过应用一些算法对其进行优化。

newArr 等于 ['7065682', '7047674']

const EncounteredData = [['7065682'], ['7065682'], ['7065682'], ['7047674'], ['7047674']];

const newArr = [];

for(let i of EncounteredData) {
    for(let j of EncounteredData) {
        if((i[0] !== j[0]) && !newArr.includes(i[0])) newArr.push(i[0]);
    }
}

console.log(newArr);

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']

const EncounteredData = [['7065682'], ['7065682'], ['7065682'], ['7047674'], ['7047674']];

const newArr = [];

for(let i of EncounteredData) {
    for(let j of EncounteredData) {
        if((i[0] !== j[0]) && !newArr.includes(i[0])) newArr.push(i[0]);
    }
}

console.log(newArr);
蓦然回首 2025-01-18 03:18:53

如果我理解正确的话,你想要一个唯一值的数组?如果是这样,那么您可以使用映射函数并将任何唯一值添加到数组中(如果该值尚不存在于数组中):

const uniqueVals = [];

EncounteredData.map((visitID) => {
  if (!uniqueVals.includes(visitID[0])) {
    uniqueVals.push(visitID[0]);
  }
});

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:

const uniqueVals = [];

EncounteredData.map((visitID) => {
  if (!uniqueVals.includes(visitID[0])) {
    uniqueVals.push(visitID[0]);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文