What is the fastest or most elegant way to compute a set difference using Javascript arrays?
I don't know if this is most effective, but perhaps the shortest:
var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];
var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});
console.log(diff); // [2]
Most efficient way to compare multiple arrays based on a fixed element and copy the largest one?
You can use sort and just return the first element of descending sorted list
const arr = [this.k1, this.k2, this.k3, this.k4]
// sort desc
const arrayHasMaxLastElement = arr.sort((a, b) => b[b.length - 1] - a[a.length - 1])[0]
How to get the difference between two arrays in JavaScript?
This answer was written in 2009, so it is a bit outdated, also it's rather educational for understanding the problem. Best solution I'd use today would be
let difference = arr1.filter(x => !arr2.includes(x));
(credits to other author here)
I assume you are comparing a normal array. If not, you need to change the for loop to a for .. in loop.
function arr_diff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));
Find the differences between 2 multidimensional arrays
Assuming there is some character (e.g. '_'
) that cannot appear in the strings, you may convert your problem to dealing with arrays of strings, for example as follows:
(I didn't compact the code, to keep some readability)
set1 = new Set(arr1.map(x => x[0]+'_'+x[1]))
set2 = new Set(arr2.map(x => x[0]+'_'+x[1]))
diff1 = arr1.filter(x => !set2.has(x[0]+'_'+x[1]))
diff2 = arr2.filter(x => !set1.has(x[0]+'_'+x[1]))
res = diff1.concat(diff2)
Compare 2 arrays in javascript and extract differences
your requirement(result) is not clear, but this will get you started.
var arr1 = [{ "329": ["45738", "45737", "45736"] }, { "307": ["45467", "45468"] }, { "355": ["47921"] }], arr2 = [{ "355": ["47921", "45922"] }, { "329": ["45738", "45737", "45736"] }, { "307": [] }];
var result = [];
arr2.forEach(obj => { var key = Object.keys(obj)[0]; var match = arr1.find(o => o.hasOwnProperty(key)); if (match) { var newObj = {}; newObj[key] = obj[key].filter(s => match[key].indexOf(s) === -1); if (!obj[key].length || newObj[key].length) result.push(newObj) } else { result.push(Object.assign({}, obj)); }});
console.log(result);
Function that returns difference of two arrays of strings in javascript
function diff(A, B) {
return A.filter(function (a) {
return B.indexOf(a) == -1;
});
}
What is the fastest or most elegant way to compute a set difference using Javascript arrays?
I don't know if this is most effective, but perhaps the shortest:
var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];
var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});
console.log(diff); // [2]
Related Topics
How to Import Jquery Using Es6 Syntax
Google Map API Uncaught Typeerror: Cannot Read Property 'Offsetwidth' of Null
How to Use JavaScript Source Maps (.Map Files)
Fixing JavaScript Array Functions in Internet Explorer (Indexof, Foreach, etc.)
Difference Between Single Quotes and Double Quotes in JavaScript
Changing the Key Name in an Array of Objects
How to Get the Browser to Navigate to Url in JavaScript
How to Find If an Array Contains a Specific String in JavaScript/Jquery
Convert Special Characters to HTML in JavaScript
How to Define Two Angular Apps/Modules in One Page
What Is the Fastest or Most Elegant Way to Compute a Set Difference Using JavaScript Arrays
Javascript: Collision Detection
Sending Message from a Background Script to a Content Script, Then to a Injected Script
Chrome 65 Blocks Cross-Origin <A Download>. Client-Side Workaround to Force Download
Using Queryselectorall(). Is the Result Returned by the Method Ordered