Remove Duplicates from an Array Based on Object Property

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

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

How to remove duplicates objects in array based on 2 properties?

You can do

const rooms = [  {    room_rate_type_id: 202,    price: 200  },  {    room_rate_type_id: 202,    price: 200  },  {    room_rate_type_id: 202,    price: 189  },  {    room_rate_type_id: 190,    price: 200  }];
let result = rooms.filter((e, i) => { return rooms.findIndex((x) => { return x.room_rate_type_id == e.room_rate_type_id && x.price == e.price;}) == i;
});
console.log(result);

Removing Duplicates in Array of Objects based on Property Values

First off, please don't use .map() when not performing a mapping operation. I've substituted the usage of .map with .forEach as the latter is more appropriate in this case.

Second, your comment //probably need to remove something here is correct - you do have to remove an item. Namely, you have to remove the duplicate item rec that was just found. To do that, you can utilise Array#splice which requires the index to be removed. You can easily get the index as the second parameter of the .forEach() callback

let arrayOfObjects = [  {first:"John", last: "Smith", id:"1234", dupes: []},  {first:"John", last: "Smith", id:"555", dupes: []},  {first:"John", last: "Jones", id:"333", dupes: []},  {first:"John", last: "Smith", id:"666", dupes: []}];

arrayOfObjects.forEach(record => { arrayOfObjects.forEach((rec, index) => {// get index ------------------^^^^^-->------------------>--------------v if(record.first == rec.first && // | record.last == rec.last && // | record.id !== rec.id){ // | record.dupes.push(rec.id); // | arrayOfObjects.splice(index, 1) //<--- remove using the index --< } });});
console.log(JSON.stringify(arrayOfObjects));

Removing duplicate objects (based on multiple keys) from array

You could use a Set in a closure for filtering.

const    listOfTags = [{ id: 1, label: "Hello", color: "red", sorting: 0 }, { id: 2, label: "World", color: "green", sorting: 1 }, { id: 3, label: "Hello", color: "blue", sorting: 4 }, { id: 4, label: "Sunshine", color: "yellow", sorting: 5 }, { id: 5, label: "Hello", color: "red", sorting: 6 }],    keys = ['label', 'color'],    filtered = listOfTags.filter(        (s => o =>             (k => !s.has(k) && s.add(k))            (keys.map(k => o[k]).join('|'))        )        (new Set)    );
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Remove duplicate values from an array of objects in javascript

You can use array#reduce and array#some.

const arr = [    {label: 'All', value: 'All'},    {label: 'All', value: 'All'},    {label: 'Alex', value: 'Ninja'},    {label: 'Bill', value: 'Op'},    {label: 'Cill', value: 'iopop'}]
var result = arr.reduce((unique, o) => { if(!unique.some(obj => obj.label === o.label && obj.value === o.value)) { unique.push(o); } return unique;},[]);console.log(result);

Remove duplicates from an array of objects based on the first 3 words of object property

For the original question of how to get 3 words, one option is to use
.split() .slice() and .join():

var firstWords = item["text"].split(" ").slice(0, 3).join(" ");

you can then do a straight replacement of currentObject.text with firstWords, from the original question:

let texts = {};
arr = arr.filter(function(currentObject) {
if (firstWords in seenNames) {
return false;
} else {
seenNames[firstWords] = true;
return true;
}
});

The update attempts this, but has 2 issues:

  • .filter(function(item)) must return true/false (as it did originally) not the item/nothing.

  • item["text"].toLowerCase().startsWith(splitItem) will always be true as splitItem is built from item["text"]

Adding the removedItems additional list to the original gives:

let arr = [{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 1
},
{
"text": "Coffee is a way of stealing time.",
"id": 2
},
{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 3
}
];

let removedItems = [];
let seenNames = {};

let filtered = arr.filter((item, index) => {
let splitItem = item["text"].split(" ").slice(0, 3).join(" ").toLowerCase();

if (splitItem in seenNames) {
// already exists, so don't include in filtered, but do add to removed
removedItems.push(item);
return false;
}

// doesn't exist, so add to seen list and include in filtered
seenNames[splitItem] = true;
return true;
});

console.log(filtered);
console.log(removedItems);

Remove duplicates from javascript objects array based on object property value

This can be done with Array.reduce(), combined with Object.values() as follows:

 var data = [
{
"John Doe": "john33@gmail.com",
},
{
"William Smith": "william65@gmail.com",
},
{
"Robert Johnson": "robert99@gmail.com",
},
{
"John Smith": "john33@gmail.com",
},
{
"James Johnson": "james8@gmail.com",
},
];

const result = data.reduce((acc, o) => {
if (!acc.map(x => Object.values(x)[0]).includes(Object.values(o)[0])) {
acc.push(o);
}
return acc;
}, []);

console.log(result);

How to remove duplicate from array of objects with multiple properties as unique?

You can put your custom logic to check for duplicates into a separate function and then iterate the original array and only copy entries when they are not duplicates.

function isDuplicate(entry, arr) {
return arr.some(x => (entry.PID == x.PID) && (entry.Week == x.Week))
}

let newArray = []
for (const entry of originalArray) {
if (!isDuplicate(entry, newArray)) { newArray.push(entry) }
}

Remove duplicates from an array based on object property?

For php >=7.0:

Since array_column works on object-arrays since PHP 7.0 you can use the following combination as suggested by @plashenkov:

$filtered = array_intersect_key($array, array_unique(array_column($array, 'someProperty')));

Full example: https://3v4l.org/IboLu#v8.0.8

class my_obj
{
public $term_id;
public $name;
public $slug;

public function __construct($i, $n, $s)
{
$this->term_id = $i;
$this->name = $n;
$this->slug = $s;
}
}

$objA = new my_obj(23, 'Assasination', 'assasination');
$objB = new my_obj(14, 'Campaign Finance', 'campaign-finance');
$objC = new my_obj(15, 'Campaign Finance', 'campaign-finance-good-government-political-reform');

$array = array($objA, $objB, $objC);
echo 'Original array:\n';
print_r($array);

/** Answer Code begins here */
$filtered = array_intersect_key($array, array_unique(array_column($array, 'name')));
/** Answer Code ends here */

echo 'After removing duplicates\n';
print_r($filtered);

Output:

Original array:
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)

[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)

[2] => my_obj Object
(
[term_id] => 15
[name] => Campaign Finance
[slug] => campaign-finance-good-government-political-reform
)

)
After removing duplicates
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)

[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)

)

The object with term_id 15 was removed as it had the same name as term_id 14.

For php <7.0:

Build a new array with the existing keys and just the name as value, use array_unique (note that it preserves keys).

Then copy every key from the original array to a new array ($filtered) (or remove everything thats not in the unique'ed array from the original key-wise).

Edit: Complete example: https://3v4l.org/SCrko#v5.6.40

class my_obj
{
public $term_id;
public $name;
public $slug;

public function __construct($i, $n, $s)
{
$this->term_id = $i;
$this->name = $n;
$this->slug = $s;
}
}

$objA = new my_obj(23, 'Assasination', 'assasination');
$objB = new my_obj(14, 'Campaign Finance', 'campaign-finance');
$objC = new my_obj(15, 'Campaign Finance', 'campaign-finance-good-government-political-reform');

$array = array($objA, $objB, $objC);

echo 'Original array:\n';
print_r($array);

/** Answer Code begins here **/

// Build temporary array for array_unique
$tmp = array();
foreach($array as $k => $v)
$tmp[$k] = $v->name;

// Find duplicates in temporary array
$tmp = array_unique($tmp);

// Build new array with only non-duplicate items
$filtered = [];
foreach($array as $k => $v)
{
if (array_key_exists($k, $tmp))
$filtered[$k] = $v;
}

/** Answer Code ends here **/

echo 'After removing duplicates\n';
print_r($filtered);

Output:

Original array:
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)

[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)

[2] => my_obj Object
(
[term_id] => 15
[name] => Campaign Finance
[slug] => campaign-finance-good-government-political-reform
)

)
After removing duplicates
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)

[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)

)

The object with term_id 15 was removed as it had the same name as term_id 14.



Related Topics



Leave a reply



Submit