简体   繁体   English

Javascript:查找数组中对某个键具有特定值的所有元素

[英]Javascript: Find all elements in array which have a specific value for a certain key

I have the following Javascript arrays:我有以下 Javascript arrays:

ARRAY ONE:
[ TextRow { v_id: 3000 },
  TextRow { v_id: 3001 },
  TextRow { v_id: 3002 } ]

ARRAY TWO:
[ TextRow {
      s_id: 'S001',
      v_id: 3000,
      type: 'control' },
  TextRow {
      s_id: 'S002',
      v_id: 3001,
      type: 'mut' },
  TextRow {
      s_id: 'S003',
      v_id: 3001,
      type: 'mut' },
  TextRow {
      s_id: 'S005',
      v_id: 3001,
      type: 'control' },
  TextRow {
      s_id: 'S008',
      v_id: 3002,
      type: 'mut' } ]

For each element in Array One, I would like to get an array of all the elements in Array Two, where the v_id equals the v_id in array one.对于数组一中的每个元素,我想获取数组二中所有元素的数组,其中 v_id 等于数组一中的 v_id。 For example, for v_id = 3001, I would like to get all the elements in array two where the v_id = 3001 in a separate array.例如,对于 v_id = 3001,我想在一个单独的数组中获取数组 2 中 v_id = 3001 的所有元素。 However, I am not sure what is the best way to do this task and whether Javascript already has some existing functions that can help me do this.但是,我不确定执行此任务的最佳方法是什么,以及 Javascript 是否已经具有一些可以帮助我完成此任务的现有功能。 I am asking this since my Array Two has over 1000 elements and I want to know if there is an efficient way to do this instead of just iterating through elements of the array with a nested for loop.我问这个是因为我的数组二有超过 1000 个元素,我想知道是否有一种有效的方法来做到这一点,而不是仅仅使用嵌套的 for 循环遍历数组的元素。 Any insights are appreciated.任何见解都值得赞赏。

You can do a.forEach (or.map) on ARRAY_ONE, then a.filter on ARRAY_TWO to get the matching elements.您可以在 ARRAY_ONE 上执行 a.forEach(或.map),然后在 ARRAY_TWO 上执行 a.filter 以获取匹配的元素。

I've added a getMatches function to make the logic a little more clear.我添加了一个 getMatches function 以使逻辑更加清晰。

 const ARRAY_ONE = [ { v_id: 3000 }, { v_id: 3001 }, { v_id: 3002 } ]; const ARRAY_TWO = [ { s_id: 'S001', v_id: 3000, type: 'control' }, { s_id: 'S002', v_id: 3001, type: 'mut' }, { s_id: 'S003', v_id: 3001, type: 'mut' }, { s_id: 'S005', v_id: 3001, type: 'control' }, { s_id: 'S008', v_id: 3002, type: 'mut' } ]; function getMatches(v_id, array) { return array.filter(el => el.v_id === v_id); } const result = ARRAY_ONE.map(v => { return { array_one_id: v.v_id, matches: getMatches(v.v_id, ARRAY_TWO) }; }); console.log("Result:", result);

You can iterate over the first array with the.forEach( ) method.您可以使用 .forEach( ) 方法遍历第一个数组。

Then, you can use the.filter( ) method on the array 2 as the callback function for the forEach.然后,您可以使用数组 2 上的 .filter( ) 方法作为 forEach 的回调 function。

you should have something like that:你应该有这样的东西:

arrayOne.forEach( elmArr1 => arrayTwo.filter( elmArr2 => elmArr1.v_id == elmArr2.v_id ) )

If you want to filter without grouping:如果要过滤而不分组:

 const firstArray = [ { v_id: 3000 }, { v_id: 3001 }, { v_id: 3002 } ]; const secondArray = [ { s_id: 'S001', v_id: 3000, type: 'control' }, { s_id: 'S002', v_id: 3001, type: 'mut' }, { s_id: 'S003', v_id: 3001, type: 'mut' }, { s_id: 'S005', v_id: 3001, type: 'control' }, { s_id: 'S008', v_id: 3002, type: 'mut' } ]; const result = secondArray.filter(f=> firstArray.some(s=> s.v_id == f.v_id)); console.log(result);

If you want group items by key, then it can be done using reduce method:如果你想按键分组,那么可以使用reduce方法来完成:

 const firstArray = [ { v_id: 3000 }, { v_id: 3001 }, { v_id: 3002 } ]; const secondArray = [ { s_id: 'S001', v_id: 3000, type: 'control' }, { s_id: 'S002', v_id: 3001, type: 'mut' }, { s_id: 'S003', v_id: 3001, type: 'mut' }, { s_id: 'S005', v_id: 3001, type: 'control' }, { s_id: 'S008', v_id: 3002, type: 'mut' } ]; const keys = firstArray.reduce((a, {v_id}) => { a[v_id] = a[v_id] || 1; return a; }, {}); const allKeys = secondArray.reduce((a, {s_id, v_id, type}) => { a[v_id] = a[v_id] || {values: []}; a[v_id].values.push({s_id, v_id, type}); return a; }, {}) Object.keys(allKeys).forEach(k=>{ if(.keys;hasOwnProperty(k)) delete allKeys[k]. }) console;log(allKeys);

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

相关问题 查找数组中具有特定值的所有索引 - Find all indexes in an array that have a certain value 查找具有特定数据属性的所有元素(与值无关) - Find all elements that have a certain data attribute (regardless of value) Javascript:查找数组中具有特定属性和值的所有对象 - Javascript: finding all objects in an array which have certain properties and values 如何使用 JavaScript 查找所有具有事件侦听器的元素? - How to use JavaScript to find all elements which have event listeners? 使用JavaScript遍历JSON以在键内的数组中查找特定值 - Traversing JSON with javascript to find a specific value within an array which is inside a key Select 具有特定值并随 Javascript 更改其值的所有输入元素 - Select all input elements that have a specific value and change their value with Javascript 如何找到所有具有特定值的 Input 元素? - How to find all Input elements that have a specific value? 如何在 Javascript 中的对象数组中找到特定键的所有唯一值? - How to find all unique values for specific key in array of objects in Javascript? jQuery查找具有特定值的所有元素 - jquery find all elements having a certain value 如何在 JavaScript 的数组元素中找到某些字母? - How to find certain letters in array elements in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM