Remove duplicate values from JS array
Quick and dirty using jQuery:
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
Removing duplicate elements from an array in Swift
You can roll your own, e.g. like this:
func unique<S : Sequence, T : Hashable>(source: S) -> [T] where S.Iterator.Element == T {
var buffer = [T]()
var added = Set<T>()
for elem in source {
if !added.contains(elem) {
buffer.append(elem)
added.insert(elem)
}
}
return buffer
}
let vals = [1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6]
let uniqueVals = uniq(vals) // [1, 4, 2, 6, 24, 15, 60]
And as an extension for Array
:
extension Array where Element: Hashable {
func uniqued() -> Array {
var buffer = Array()
var added = Set<Element>()
for elem in self {
if !added.contains(elem) {
buffer.append(elem)
added.insert(elem)
}
}
return buffer
}
}
Or more elegantly (Swift 4/5):
extension Sequence where Element: Hashable {
func uniqued() -> [Element] {
var set = Set<Element>()
return filter { set.insert($0).inserted }
}
}
Which would be used:
[1,2,4,2,1].uniqued() // => [1,2,4]
Remove duplicate from array of objects based on value of properties in JavaScript
You can use Map to club values by name and in case there are two values with same name just use the one without type = "new"
let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
function getUnique(arr){
let mapObj = new Map()
arr.forEach(v => {
let prevValue = mapObj.get(v.name)
if(!prevValue || prevValue.type === "new"){
mapObj.set(v.name, v)
}
})
return [...mapObj.values()]
}
console.log(getUnique(someArray))
Remove duplicates from an array in BigQuery
If you want to deduplicate an array in BigQuery, 1) you first have to flatten the array. 2) Subsequently, deduplication can be done as usual (e.g. using distinct or grouping etc.) 3) Finally, to return an array (as the original schema), we can group on the primary key and use ARRAY_AGG
.
1) Flattening of the array is done using the UNNEST operator.
The UNNEST operator takes an ARRAY and returns a table, with one row for each element in the ARRAY.
In combination with a CROSS JOIN
, we retrieve a row for each array record. (Note, the comma implies a CROSS JOIN
, see docs)
SELECT
order_id,
item.sku AS item_sku,
item.price AS item_price
FROM data, UNNEST(items) AS item
2) Using DISTINCT
we retrieve unique records.
SELECT DISTINCT
order_id,
item.sku AS item_sku,
item.price AS item_price
FROM data, UNNEST(items) AS item
3) We can group on the primary key (order_id
) and use ARRAY_AGG to return the results as array. I.e., returning the original schema.
SELECT
order_id,
ARRAY_AGG(
STRUCT(
item_sku AS sku,
item_price AS price
)
) AS item
FROM flattened
GROUP BY order_id
Full reproducible example
WITH data AS (
SELECT
123 AS order_id,
[
STRUCT(
'ABC' AS sku,
5.99 AS price
),
STRUCT(
'ABC' AS sku,
5.99 AS price
),
STRUCT(
'XYZ' AS sku,
19.99 AS price
)
] AS items,
UNION ALL
SELECT
456,
[
STRUCT(
'ABC' AS sku,
5.99 AS price
)
]
UNION ALL
SELECT
789,
[
STRUCT(
'XYZ' AS sku,
19.99 AS price
)
]
),
flattened AS (
SELECT DISTINCT
order_id,
item.sku AS item_sku,
item.price AS item_price
FROM data, UNNEST(items) AS item
)
SELECT
order_id,
ARRAY_AGG(
STRUCT(
item_sku AS sku,
item_price AS price
)
) AS item
FROM flattened
GROUP BY order_id
how to remove first element of duplicate in an array of objects
This will give you an array with the last value for duplicated elements, in preserved order. If you want exactly the 2nd one you need an extra flag
array=[{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]
const obj = array.reduce ( (acc,cur,index) => {
acc[cur.id] = {index:cur};
return acc;
},{});
const output = Object.values(obj).sort( (a,b) => a.index - b.index).map( ({index:val}) => val )
console.log(output)
I need to remove duplicate products in an Array and sum their quantity in JavaScript
You can try the below approach to achieve this. Create an object (productsCheck
in my case) and loop through the productsAll
array. If the name already exists in the object then add the quantity, else simple add the product to your object ( i.e productsCheck
)
Working code:
const productsAll = [{
name: 'Product 1',
ean: '1112223334445',
sku: '4445',
product_id: '70604566',
quantity: 1,
},
{
name: 'Product 1',
ean: '1112223334445',
sku: '4445',
product_id: '70604566',
quantity: 3,
},
{
name: 'Product 2',
ean: '1112223334446',
sku: '4446',
product_id: '60404533',
quantity: 2,
},
{
name: 'Product 3',
ean: '1112223334447',
sku: '4447',
product_id: '30504512',
quantity: 8,
},
];
const productsCheck = {}
productsAll.forEach(product => {
if (product.name in productsCheck) {
productsCheck[product.name].quantity += product.quantity
} else {
productsCheck[product.name] = product
}
})
console.log(productsCheck)
Remove duplicate objects from an Array
This one will assign this.subMenuItems
an array containing only the first instance of each item because indexOf
returns the first index where the object is found.
this.subMenuItems = this.items.filter((item, index, self) => self.indexOf(item) === index);
Related Topics
How to Prevent the Backspace Key from Navigating Back
Elliptic Curve Cryptography with Sjcl in Js and Openssl in Ruby
Google Maps Places API V3 Autocomplete - Select First Option on Enter
Angular 2 Optional Route Parameter
How to Enable Touch on Multiple Buttons Simultaneously in React Native
Event.Preventdefault() VS. Return False (No Jquery)
How to Set State Inside a Useeffect Hook
Double Negation (!!) in JavaScript - What Is the Purpose
How to Return Multiple Lines Jsx in Another Return Statement in React
How to Set Header and Options in Axios
How to Detect Faces Using Ruby
When to Use Node.Js VS Sinatra VS Rails
Creating Range in JavaScript - Strange Syntax
Playing Sound Notifications Using JavaScript