简体   繁体   English

我有两个 arrays,第一个数组中的值与第二个数组的 ID 字段中的值相同

[英]I have two arrays, with the same values in the first array as in the ID fields of the second array

What I want to achieve is to have a third array with an output seen at the bottom from two other arrays.我想要实现的是让第三个阵列在底部看到一个 output,另外两个 arrays。

Now the key here is that it is basically a filter based on the Object.keys array matches from the mapped array.现在这里的关键是它基本上是一个基于映射数组中 Object.keys 数组匹配项的过滤器。 Meaning any id that exists in the mapped array and the Object.keys array should go into the third array.这意味着映射数组中存在的任何 id 和 Object.keys 数组应该 go 进入第三个数组。

The more I think about the more it seems I need to do a filter on the mapped array, just not 100% certain how to achieve that properly.我越想越觉得我需要对映射数组进行过滤,只是不能 100% 确定如何正确地实现它。 I did just try a filter now seen below and it returns an empty array.我只是尝试了一个现在在下面看到的过滤器,它返回一个空数组。

I have also tried using a findIndex to find the matching id's but it doesn't seem to find matches.我也尝试过使用 findIndex 来查找匹配的 ID,但它似乎没有找到匹配项。

Any help would be greatly appreciated任何帮助将不胜感激

Thanks Kindly谢谢亲切

    const mapped = data.allProduct.map((item: any) => ({
      slug: item.slug.current,
      id: item.stripeId,
    }));

   // What I have tried

    const foundMapped = mapped.findIndex(
      (element: any, index: any) => element.id === Object.keys(cartDetails)[index]
    );

    const test = mapped.filter(
      (item: any, index: any) => item.id === Object.keys(cartDetails)[index]
    );
    

this is Object.keys(cartDetails)这是Object.keys(cartDetails)

[
    "price_1KhMhhF2lAt0poJW42giHxDs",
    "price_1KVisMF2lAt0poJWnd9LGheT",
    "price_1KVisMF2lAt0poJWnd9fkfkf",
]

this is variable mapped from above这是从上面映射的变量

[
    {
        "slug": "the-void",
        "id": "price_1KVisMF2lAt0poJWnd9LGheT"
    },
    {
        "slug": "magic-wonders-artwork",
        "id": "price_1KhMhhF2lAt0poJW42giHxDs"
    },
    {
        "slug": "other-two",
        "id": "price_1KVisMF2lAt0poJWnd9fkfkf"
    },
    {
        "slug": "test-test",
        "id": "price_1KVisMF2lAt0poJWnd9fjff"
    }
]

Third Array Output第三阵列 Output

[
  {
    id: "price_1KhMhhF2lAt0poJW42giHxDs",
    slug: "the-void"
  },
  {
    id: "price_1KVisMF2lAt0poJWnd9LGheT",
    slug: "magic-wonders-artwork"
  },
  {
    id: "price_1KVisMF2lAt0poJWnd9fkfkf",
    slug: "other-two"
  },
]

just gotta check if the id is in the array of the keys in your filter predicate:只需检查id是否在filter谓词的键数组中:

const keys = Object.keys(cartDetails);
const toFilter = data.allProduct.map((item: any) => ({
  slug: item.slug.current,
  id: item.stripeId,
}));

const filtered = toFilter.filter(i => keys.includes(i.id))

though I'm not sure why you wouldn't just skip the object keys to array mapping and just check the original object, which is more efficient than checking if an item is in an array:虽然我不确定你为什么不跳过 object 键到数组映射并只检查原始的 object,这比检查一个项目是否在数组中更有效:

const filtered = toFilter.filter(i => !!cartDetails[i.id])

or或者

const filtered = toFilter.filter(i => cartDetails[i.id] !== undefined)

if cartDetails can / might include valid falsey values like 0, empty string or false如果cartDetails可以/可能包含有效的假值,如 0、空字符串或假

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个数组,长度相同。 如果第一个数组的值等于true,则从第二个数组获取值 - Two arrays, same length. If first array values equals true get values from second array JavaScript 比较两个 arrays 并将在第二个数组中具有相同属性值的项目推送到第一个数组 - JavaScript compare two arrays and push items to the first array that have the same property value in the second array 我有两个数组,我需要打印第一个数组中的第一个元素和第二个数组中的第一个元素,依此类推 - I have two arrays, i need to print first element from first array and first element from second array and so on 如何将两个数组组合成一个对象,使第一个数组的值为键,第二个数组的值为值 - How to combine two arrays into one object so that the values of the first array are keys, and the values of the second array are values 比较两个 arrays,从第一个到第二个有一个属性,其中元素的 id 相同 - Comparing two arrays, have one property from first to second where id of element is same 比较两个数组并从第二个数组中不包含的第一个数组中获取值 - compare two arrays and take values ​from the first array not contained in the second array 如何通过比较 arrays 中的 id 将第一个数组中的值分配给第二个数组中的新键。 在 javascript - How can i assign value in first array to new key in second array by comparing id in both arrays . in javascript 两个数组,根据第一个的重复值将第二个数组分组,并形成一个字符串 - two arrays, group second array based on repetitive values of first and form a string Angular - 比较两个 arrays 具有相同数量的对象但具有不同的值,并返回与第二个数组的差异 - Angular - Compare two arrays with same number of objects but with different values and return the difference from second array 如何从数组数组中提取在第一个字段中具有相同值的所有数组? - How can I extract from an array of arrays all the arrays that have the same value in their first field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM