简体   繁体   English

如何获取数组A和数组B的所有项目并将其保存在数组C中? 使用JavaScript而不使用循环

[英]How to get all items that are both is array A and B and save them in array C? using javascript without using loops

lets say I have 可以说我有

var A = [x, y, z]
var B = [1, 2, z, 3, x]

How can I get items present both in A and B like so 我如何才能像这样在A和B中同时显示商品

var C = [AB]

Without having to loop through both arrays to check for each iteration 无需遍历两个数组来检查每次迭代

You can use Array.prototype.filter() and Array.prototype.includes() to determine if both arrays contain the same element, Set to remove duplicate elements from resulting array 您可以使用Array.prototype.filter()Array.prototype.includes()确定两个数组是否包含相同的元素, Set为从结果数组中删除重复的元素

 var A = ["x", "y", "z"] var B = [1, 2, "z", 3, "x"] var C = [...new Set( [...A, ...B].filter(prop => A.includes(prop) && B.includes(prop))) ]; console.log(C); 

With duplicates: 重复:

var C = [...A, ...B]
// Result: C = ["x", "y", "z", 1, 2, "z", 3, "x"]

Without duplicates: 没有重复:

var C = [...A, ...B].filter((el, i, a) => i === a.indexOf(el))
// Result: C = ["x", "y", "z", 1, 2, 3]

Update: if you want the intersection of both arrays: 更新:如果要两个数组的交集:

var C = A.filter(function(n) {
    return B.indexOf(n) !== -1;
});
// Result: C = ["x", "z"]

 let a = new Set(["x", "y", "z"]); let b = new Set([1, 2, "z", 3, "x"]); let intersection = [...a].filter(x => b.has(x)); console.log(intersection); 

Sort each array then merge? 对每个数组排序然后合并?

/* sort DESC */
A.sort(function(a,b){ return b-a; })
B.sort(function(a,b){ return b-a; })

then use: 然后使用:

function union_destructive(a, b)
{
  var result = [];
  while( a.length > 0 || b.length > 0 )
  {  
     if      (a[0] > b[0] ){ result.push(a.shift()); }
     else if (a[0] < b[0] ){ result.push(b.shift()); }
     else /* they're equal */
     {
       result.push(a.shift());
       b.shift();
     }
  }

  return result;
}

( Simplest code for array intersection in javascript ): javascript中数组交集的最简单代码 ):

Thats O(n log n). 多数民众赞成O(n log n)。

暂无
暂无

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

相关问题 如何使用jQuery将节点的所有子节点作为javascript中的数组项 - how to get all the children of a node as items of an array in javascript using jQuery 如何在不使用循环的情况下将数组转换为 Javascript 中的对象? - How to convert an array to an object in Javascript without using loops? FreeCodeCamp:使用For循环遍历数组的所有项目 - FreeCodeCamp: Iterating Through All an Array's Items Using For Loops 如何解释使用 for 循环在 JavaScript 中反转数组 - How to explain reversing an array in JavaScript using for loops 如何在不使用for循环的情况下从JavaScript数组的每个数组中返回两个最大数? - how to return the two largest number from each array of an array in JavaScript without using for loops? 如何将包含连字符/破折号(?)的所有类应用于任何 DOM 元素并将它们保存到 JavaScript 的数组中? - How to get all classes containing hyphen/dash(?) applied to any DOM element and save them into array in JavaScript? Javascript使用带有数组列表的for循环 - Javascript using for loops with array lists JavaScript:将超时保存在数组中并全部清除 - JavaScript: save timeouts in array and clear them all 在不使用嵌套for循环的情况下迭代JavaScript数组 - Iterate over a JavaScript array without using nested for-loops 如何使用 JavaScript 获取数组项中的最后一个元素 - How the get the last element in an array items using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM