查找动态数组的对象元素中的常见元素Javascript [英] Find common elements within dynamic array's object elements Javascript

查看:111
本文介绍了查找动态数组的对象元素中的常见元素Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关这个话题的问题很多,但是我找不到直接解决我的问题的答案。



这是一个: 使用Javascript查找1数组中的常见元素



第一个区别是我有一个不同类型的数组,其元素是具有键值对的对象,其中key是字符串,值是整数数组。



第二个区别是数组是动态的,这意味着有时它可能有零个元素,而其他时候它可能有3个对象元素。 / p>

我正在共享下面的示例数组:

  const array = [ 
{key1:[1,2,3]},
{key2:[2,3,4]},
{key3:[2,5, 6]},
];

第三个区别是元素顺序 / strong>,以便最终结果应输出所有后续数组中存在的第一个元素的值。



结果应该是:

  const result = [2]; 

由于 2 是这三个元素的唯一通用整数。 p>

由于提到的数组有时候可能只有1或2个或没有元素,这些情况应该被记录。



编辑1 :按照注释中的要求数组的值是唯一的

解决方案

由于一个值​​只能在数组中出现一次,您可以连接数组,计算出现次数,并过滤不等于原始数组长度的数组。



  const findRecuring =(array)=> [... [] .concat(... array.map((o)=> Object.values(o)[0]))//组合到一个数组.reduce((m,v)=> m.set(v,(m.get(v)|| 0)+ 1),new Map())//计算地图中所有值的出现] //将地图转换为数组的键/值对filter(([k,v])=> k); //提取键/ **测试用例** / console.log('几个:',findRecuring([{key1:[6,1,2,3,8]},{key2:[ ()();;(); key1:[9,0,11]},{key2:[2,6,3,4,8]},{key3:[2,5,6,8]},])。 ());  


There were a lot of questions asked about this topic, but I couldn't find the answer that addressed directly the issue I am having.

Here is one: Find common elements in 1 array using Javascript

The first difference is that I have a different type of array, its elements are objects with key-value pair, where key is the string and the value is an array of integers.

The second difference is that array is dynamic meaning that sometimes it may have zero elements and the other times it may have 3 object elements.

I am sharing the sample array below:

const array = [
  {"key1":[1,2,3]},
  {"key2":[2,3,4]},
  {"key3":[2,5,6]},
];

The third difference is that the order of elements matters so that final result should output the values of the first element that exist in all subsequent arrays.

The result should be:

const result = [2];

Since 2 is the only common integer of these three elements.

As mentioned array sometimes might have just 1 or 2 or no elements in it and those cases should be accounted.

Edit 1: as asked in the comments the values of array are unique

解决方案

Since a value can appear in array only once, you can concat the arrays, count the number of appearances, and filter our those that are not equal to the length of the original array.

const findRecuring = (array) => [...
  [].concat(...array.map((o) => Object.values(o)[0])) // combine to one array
 .reduce((m, v) => m.set(v, (m.get(v) || 0) + 1), new Map()) // count the appearance of all values in a map
 ] // convert the map to array of key/value pairs
 .filter(([k, v]) => v === array.length) // filter those that don't appear enough times
 .map(([k, v]) => k); // extract just the keys

/** Test cases **/
console.log('Several:', findRecuring([
  {"key1":[6,1,2,3,8]},
  {"key2":[2,6,3,4,8]},
  {"key3":[2,5,6,8]},
]).join());

console.log('None: ', findRecuring([
  {"key1":[9,0,11]},
  {"key2":[2,6,3,4,8]},
  {"key3":[2,5,6,8]},
]).join());

这篇关于查找动态数组的对象元素中的常见元素Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆