Remove Duplicate Values from Js Array

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

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);

console.log(unique); // ['a', 1, 2, '1']

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 duplicate values from flat ARRAY

You can use ES6 Set() function to remove the duplicates

const newArray = [...new Set(arrayWithDuplicates)];

How to remove all duplicates from an array of objects?

A primitive method would be:

const obj = {};

for (let i = 0, len = things.thing.length; i < len; i++) {
obj[things.thing[i]['place']] = things.thing[i];
}

things.thing = new Array();

for (const key in obj) {
things.thing.push(obj[key]);
}

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)

Removing duplicates from an array leaves duplicate values in the end result

Solution:

function removeDuplicate(arr) {
var i = 0;
var j = 1;
while (j < arr.length) {
if (arr[i] === arr[j]) {
j++;
} else {
arr[++i] = arr[j];
j++;
}
}
for(let k = arr.length; k > i+1; k--){
arr.pop()
}

return arr;
}

const ans = removeDuplicate([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])
console.log(ans);


Related Topics



Leave a reply



Submit