Create array of the duplicates from array containing duplicates in javascript? [duplicate] - javascript

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
How to keep Duplicates of an Array
(4 answers)
Create new array with only the duplicate items in Javascript using for loops
(4 answers)
lodash: Get duplicate values from an array
(13 answers)
Closed 1 year ago.
For an array of objects ["a","b","a","c","d","b"] I'd like to get an array of the duplicates:
["a","b"].
Is there a way to do this efficiently, similar to set ([...new Set(myArray)];)?

You could still use a Set and filter the array by checking the existence.
const
items = ["a", "b", "a", "c", "d", "b"],
duplicates = items.filter((s => v => s.has(v) || !s.add(v))(new Set));
console.log(duplicates);

Related

Remove all elements which occurs more than one time [duplicate]

This question already has answers here:
Return numbers which appear only once (JavaScript)
(4 answers)
Find single occurrence of an element in an array
(3 answers)
Deleting both values from array if duplicate - JavaScript/jQuery
(9 answers)
Closed 8 months ago.
Suppose I have an array like [1,1,2,2,3,4]
I want to get [3,4] by using a function like function answer(data,n)
I would reduce() down to a map with the elements as keys, and boolean values to indicate whether the element has been seen before. Then it's just a matter of filtering out the entries that are duplicates:
const data = [1, 1, 2, 2, 3, 4];
const result = [
...data.reduce((a, v) => a.set(v, a.has(v)), new Map())
].filter(([_, v]) => !v).map(([k]) => k);
console.log(result);

Trying to concat an array of multiple array into one array [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
How to flatten nested array in javascript? [duplicate]
(27 answers)
Closed 2 years ago.
I try to concat Array of multiple Arrays like this
`let soldNumber = [
[1,2],
[5,7],
[35,67],
...
]`
to recieve this let soldNumber = [1,2,5,7,35,67,...]
soldNumber.flat()
Array.prototype.flat()
let soldNumber = [[1,2], [5,7], [35,67]];
let newSoldNumber = soldNumber.flat();

Javascript: Get the subsets of objects from an array of objects [duplicate]

This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 2 years ago.
I have an array of objects like this:
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
I am trying to get the subset of the array objects like below in Javascript: I also need to add a 10 to value2.
var arrayOne = [{date:'01/01/2017',value2:300}, {date:'02/01/2017',value2:330},{date:'03/01/2017',value2:330}]
Any suggestions would be greatly appreciated.
You could use map to iterate and select
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
console.log(array.map(({ date, value2 }) => ({ date, value2: value2 + 10 })))

Merge multiple object values of array into 1 [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 2 years ago.
This is my array:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3'
]
If want to check, if it has similar objects then I want it to be merged into one and return me array as:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
]
How is this possible in JavaScript or TypeScript?
Use Set to remove duplicate values :
array = [ 'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3' ];
var result = [...new Set(array)];
console.log(result);

Referencing / Parsing an object [duplicate]

This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
How to loop through a plain JavaScript object with the objects as members
(28 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Iterate through object properties
(31 answers)
How to iterate (keys, values) in JavaScript? [duplicate]
(11 answers)
Closed 5 years ago.
Very simple question here, I have the following variable. set up in a format like so {item number: quantity, item number, quantity....}
var x = {4214061251:1,2497227651:2,4214140739:2};
My question is simply how do i loop through this properly to access each element.
I would like to be able to access the item numbers and quantities separately, how can I achieve this?
var x = {4214061251:1,2497227651:2,4214140739:2};
// Array of all keys 4214061251, 2497227651 ...
var keysArr = Object.keys(x);
console.log(keysArr);
// Iterate on keys
keysArr.forEach(
(el) => {
// To access keys
console.log("Key: "+el);
// To access values instead
console.log("Value: "+x[el]);
}
)

Categories