Deleting both values from array if duplicate using lodash [duplicate] - javascript

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 5 years ago.
how to delete the duplicate value from the array.
var list =[1,1,5,5,4,9]
my result will be
var list =[4,9]
how can I do by using lodash

You could check the index and last index of the actual value.
var list = [1, 1, 5, 5, 4, 9],
result = list.filter((v, _, a) => a.indexOf(v) === a.lastIndexOf(v));
console.log(result);

You can do
var list =[1,1,5,5,4,9];
let result = list.reduce((a, b) =>{
a[b] = a[b] || 0;
a[b]++;
return a;
}, []).map((e, idx) => e==1? idx: undefined).filter(e => e);
console.log(result);

you could use _.uniqBy()
_.uniqBy(list ,function(m){
return list.indexOf(m) === list.lastIndexOf(m)
})

Related

how to filter an array by index which is given as an array [duplicate]

This question already has answers here:
How to get an array of values based on an array of indexes?
(6 answers)
Closed 1 year ago.
I want to filter this array by index?
arr = [1,2,3,4,5,6];
I am trying to use
arr.filter(x => x === [2,3])
but it does not seems to work?
I want only [2, 3]rd index?
You need map here rather than filter:
arr = [11,22,33,44,55,66];
indexes = [2,3]
result = indexes.map(i => arr[i])
console.log(result)
Note that this returns values in the indexes order, but if indexes are unsorted and you need values in the source order, then you indeed need filter. Consider:
arr = [11,22,33,44,55,66];
indexes = [4,2]
result1 = indexes.map(i => arr[i])
result2 = arr.filter((_,i) => indexes.includes(i))
console.log(result1)
console.log(result2)

Is there any way to plus multi array number in javascript? [duplicate]

This question already has answers here:
How to calculate the sum of multiple arrays?
(6 answers)
Closed 1 year ago.
I have an array. With each item in array is an array number. And the length of each array is the same. For example:
var data = [[1,2,4,1], [2,2,1,3], [1,1,2,2], ...]
And the result I want to have:
=> res = [4, 5, 7, 6]
res is the result of adding arrays according to the corresponding index. And of course my data may also contain lots of items.
I have referenced through the lodash.unzipWith. But it doesn't seem viable. With any advice. please let me know. Sorry for my weak English
You can use reduce and write something like this, without lodash or anything
const data = [[1,2,4,1], [2,2,1,3], [1,1,2,2]]
const sumArrs = (arrs) => {
return arrs.reduce((prev, curr) => {
return curr.map((num, i) => num + (prev[i] || 0))
}, [])
}
console.log(sumArrs(data))

What's the best way to find intersection in 2 arrays and what algorithmic complexity does each of the ways have? [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 2 years ago.
with some
function test(arr1,arr2){
return arr1.filter(el1=>{
return arr2.some(el2=>{
return el2 === el1
})
})
}
console.log(test([1,2,3,4],[3,4,5,6]))
with includes
function test(arr1,arr2){
return arr1.filter(item => arr2.includes(item));
}
console.log(test([1,2,3,4],[3,4,5,6]))
Maybe there are better way to solve this task?
You could take a Set for one array and filter the other one.
Basically you need one loop for every array.
function test(arr1, arr2) {
return arr1.filter(Set.prototype.has, new Set(arr2));
}
console.log(test([1, 2, 3, 4], [3, 4, 5, 6]))
A better way would be using a Set to save the numbers in the first array.
Then, iterate over the second array using .forEach and add each item that is in the set:
function getIntersection(arr1 = [], arr2 = []) {
const set = new Set(arr1);
const intersection = [];
arr2.forEach(num => {
if(set.has(num)) intersection.push(num);
});
return intersection;
}
console.log( getIntersection([1,2,3,4], [3,4,5,6]) );

Create Array and fill with index plus one in JavaScript [duplicate]

This question already has answers here:
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
How to create an array containing 1...N
(77 answers)
Closed 2 years ago.
I am trying to create an Array using new Array() and filling with index + 1 but somehow, though array is creating successfully but values are not filling correctly.
Code -
var aa = (new Array(4)).map((x, index) => {
return index + 1;
});
console.log(aa);
Output - [undefined, undefined, undefined, undefined]
Expected - [1, 2, 3, 4]
Let me know what I am doing wrong here.
map only visits entries that actually exist, it skips gaps in sparse arrays.
There are various ways to do this:
You can use Array.from and its mapping callback:
const as = Array.from(Array(4), (_, index) => index + 1);
console.log(as);
You can use a simple for loop:
const as = [];
for (let index = 1; index <= 4; ++index) {
as.push(index);
}
console.log(as);
You can use fill and then map:
const as = Array(4).fill().map((_, index) => index + 1);
console.log(as);
A variant of fill+map, you can use spread as Igor shows.
[...Array(4)].map((v, i) => i + 1)

JS - filtering certain value from the array [duplicate]

This question already has answers here:
Why does Array.filter(Number) filter zero out in JavaScript?
(8 answers)
Closed 4 years ago.
I have a problem on which I was stuck for awhile but I already solved it but I still need insight as to why it occurs.
The problem wants me to subtract one list from another and return the resulting
array_diff([1,1,2],[1]) == [1].
So I decided to use array.filter() for the problem and this is what I came up with:
function array_diff(a, b) {
for (i in b){
a = a.filter(function(c){ if (c != b[i]){ return c; }});
}
return a;
}
It runs fine until the array includes zero.
For example: array_diff([0,3,4],[3]). I got [4] instead of [0,4]. Why is that?
My solution later is to map a new array and filter null val and that works.
function array_diff(a, b) {
for (i in b){
a = a.map(function(c){ if (c != b[i]){ return c; }});
}
var filtered = a.filter(function (el) {
return el != null;
});
return filtered;
}
But why does filter work like that. I would like to know.
Here is a shorter way to do it, using filter and includes:
const a = [0, 1, 2, 3, 4, 5];
const b = [0, 1, 0, 3, 0, 5];
const arrayDiff = (a, b) => a.filter(elem => !b.includes(elem));
console.log(arrayDiff(a, b));
The callback to the filter method needs to return a Boolean value, which is used to decide whether to keep the element or not. Your code:
a = a.filter(function(c){ if (c != b[i]){ return c; }});
Is returning the element itself. Since JS expects a boolean, it is converting the number to a bool. In particular, 0 is converted to false, so is excluded.
Just do this instead:
a = a.filter(function(c){ return (c != b[i]);});

Categories