jQuery Array Difference [duplicate] - javascript

This question already has answers here:
Compare 2 arrays which returns difference
(10 answers)
Closed 5 years ago.
This is isn't really a question, it's a solution but I wanted to post it because I've seen it come up frequently. Feel free to suggest improvements though. I'll update my Fiddle with the results.
Using jQuery, this compares 2 arrays and outputs the differences in the two.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var foo = [];
var i = 0;
jQuery.grep(array2, function(el) {
if (jQuery.inArray(el, array1) == -1) foo.push(el);
i++;
});
alert(" the difference is " + foo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The library Underscore.js is very helpful for stuff like this.
http://underscorejs.org/#difference
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]

Related

sorting an array in javascript, such that new starting and ending items but they have the same order [duplicate]

This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
Closed 1 year ago.
let's say I have this array of number in reverse order:
var days = [6, 5, 4, 3, 2, 1, 0];
and I have this variable:
var startingDay = 4;
How can I sort the array so that the array is like this
console.log(days == [4, 3, 2, 1, 0, 6, 5]);
>> true
??
Thanks!
You can rotate the array by slicing and concatenating the two parts.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
var idx = days.indexOf(day);
var res = days.slice(idx).concat(days.slice(0, idx));
console.log(res);
You can also use unshift and splice to modify the array in-place.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
days.unshift(...days.splice(days.indexOf(day), days.length));
console.log(days);

How to keep the array 'structure' inside a string in JavaScript [duplicate]

This question already has answers here:
Preserve Nested Array Structure When Converting to String, JavaScript
(2 answers)
Closed 2 years ago.
my question is, if I have an array in javascript, let's say :
let x = [1, 2, 3, [4, 5, 6]]
And I want to transform it into a string that would look like this : y = "[1, 2, 3, [4, 5, 6]]", how should I do it?
I tried this things:
let a = x.toString();
let b = y.toLocaleString();
let c = new String(x);
But the problem with it is that, all of them look like this:
"1, 2, 3, 4, 5, 6"
So, it completely removes the '[]'.
How can I keep the array inside the string like this :
"[1, 2, 3, [4, 5, 6]]"
with the []
Does this helps?
let y = [1, 2, 3, [4, 5, 6]];
var x = JSON.stringify(y);
console.log(typeof(x));
console.log(JSON.stringify(y));

Separate arrayof obj in others sequentially [duplicate]

This question already has answers here:
Group an array to form three arrays based on the index
(3 answers)
Closed 3 years ago.
Trying to do a method to re-arrange an array to print in columns:
I have:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I need to have:
b = [1, 4, 7];
c = [2, 5, 8];
d = [3, 6, 9];
...
where numbers of others colums(b, c, d) could be variable.
Use an Array#reduce call that returns an array of arrays, and determine which array each item belongs to, using the remainder of the index.
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var listCount = 3;
var [b, c, d] = a.reduce(function(lists, item, index) {
lists[index % listCount] = lists[index % listCount] || [];
lists[index % listCount].push(item);
return lists;
}, []);
console.log(b);
console.log(c);
console.log(d);

Dividing array into sub-arrays based on number of groups

This is kind of confusing so I will just demonstrate. I would like to create a function that takes an array like this:
[1, 2, 3, 4, 5, 6, 7]
...and returns an array of arrays like this:
[[1, 5], [2, 6], [3, 7], [4]]
assuming that the user wanted 4 groups. Notice that the elements are added to each group before 5th element is added back to the first.
Is there a simple way to do this? My project is using webpack, so I am open to ES6+ or even lodash. Thanks!
My proposal is:
var numGroups= 4;
var result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(function(acc, ele, idx) {
var i = idx % numGroups;
(acc[i] == undefined) ? acc[i] = [ele] : acc[i].push(ele);
return acc;
}, []);
console.log(result);

How to find common elements only between 2 arrays in Angular 2 [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Closed 5 years ago.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1.)If elements are equals,show that common elemenst in output
2.)The output(common elements) should be array form
Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = array1.filter(function(v) { // iterate over the array
// check element present in the second array
return array2.indexOf(v) > -1;
// or array2.includes(v)
})
console.log(res);

Categories

Resources