Merge multiple object values of array into 1 [duplicate] - javascript

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);

Related

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

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);

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 })))

Flatten array with arrays [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
Say I have an array with arrays inside it. I would like to flatten it and get a single array with all the values.
let arrWithArrs = [[1,2,3], [4,5,6]];
let array = arrWithArrs.map(arr => ...arr);
This obviously doesn't work but I would like to know how to make it work.
The wanted outcome would be
array = [1,2,3,4,5,6];
You can perform a reduce on the array to combine each of the arrays into a single array like so:
arrWithArrs.reduce((result, lst) => [...result, ...lst], []);

I have my array as arr=[1,2,3,4]. I pushed an element in it arr.push(5). Now I need to display my array as [5,4,3,2,1]. Any Idea? [duplicate]

This question already has answers here:
How can I reverse an array in JavaScript without using libraries?
(36 answers)
Closed 7 years ago.
I have an array as:
var arr = [1,2,3,4]
//Push and element to array
arr.push(5)
//Now, arr = [1,2,3,4,5]
I need to display my array as
Elements in array arr is:
5,1,2,3,4
Arr.reverse() gives me
5,4,3,2,1.
But i need
5,1,2,3,4
Simply use Array.prototype.reverse():
console.log(arr.reverse());
References:
Array.prototype.reverse().
This is the code you need to use :
arr.reverse();
Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp

Categories