Remove duplicate item from array Javascript
Date : March 29 2020, 07:55 AM
this one helps. Why do it the hard way, it can be done more easily using javascript filter function which is specifically for this kind of operations: var arr = ["apple", "bannana", "orange", "apple", "orange"];
arr = arr.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
---------------------
Output: ["apple", "bannana", "orange"]
|
Remove duplicate item from multidimensional array
Tag : php , By : user184406
Date : March 29 2020, 07:55 AM
To fix the issue you can do Best practice (and a technique that you will find implemented in Stackoverflow questions every day) is to assign temporary keys, overwrite any pre-existing subarrays based on the temporary keys, then remove the temporary keys at the end. This avoids having to do iterated lookups. For highest efficiency in your codes, try to minimize iterated function calls. foreach($guardian->guardians as $subarray){
$result[$subarray->id]=$subarray; // assign temporary keys
}
$guardian->guardians=array_values($result); // re-declare and remove temporary keys (re-index the subarrays)
var_export($guardian);
$guardian->guardians=array_values(array_column((array)$guardian->guardians,NULL,'id'));
var_export($guardian);
foreach($guardian->guardians as $subarray){
if(!isset($result[$subarray->id])){
$result[$subarray->id]=$subarray; // only store if first occurrence of id
}
}
$guardian->guardians=array_values($result); // re-index
var_export($guardian);
|
Duplicate Array item x number of times based off a value inside each array item
Date : March 29 2020, 07:55 AM
Does that help I don't think map is a good choice as it does one value to one value mapping. If you have lodash available you could use flatMap in which case your return[item, item] idea would work. If not then perhaps use reduce. Something like: let cardMap = cards.reduce(function (acc, item){
for (let i = 0; i < item.matches; i++) {
acc.push(item);
}
return acc;
}, []);
|
Remove item with duplicate ID from multidimensional array with criterion which to keep
Date : March 29 2020, 07:55 AM
hope this fix your issue You can solve this in a concise way via Array.reduce and some ES6 destructuring: const data = [{id:10,coords:[[15,69],[16,85],[16,34]]},{id:10,coords:[[15,69],[16,63]]},{id:20,coords:[[15,69],[16,85],[16,34]]},{id:30,coords:[[15,69],[16,85],[16,34]]},{id:50,coords:[[15,69],[16,85],[16,34]]}]
const result = data.reduce((r, {id, coords}) => {
r[id] = (r[id] || []).length < coords.length ? {id, coords} : r[id]
return r
}, {})
console.log(Object.values(result))
const data = [{id:10,coords:[[15,69],[16,85],[16,34]]},{id:10,coords:[[15,69],[16,63]]},{id:20,coords:[[15,69],[16,85],[16,34]]},{id:30,coords:[[15,69],[16,85],[16,34]]},{id:50,coords:[[15,69],[16,85],[16,34]]}]
const result = data.reduce((r, {id, coords}) =>
(r[id] = (r[id] || []).length < coords.length ? {id, coords} : r[id]) && r, {})
console.log(Object.values(result))
|
Remove array item if it has a duplicate id
Date : March 29 2020, 07:55 AM
I hope this helps . I have an array with the following values: , One liner - Using Set components = [{componentName:"hero",componentId:"hero-5856bvC6"},{componentName:"features",componentId:"features-iAnIKgJN"},{componentName:"features",componentId:"features-ncf3WpTA"},{componentName:"features",componentId:"features-iAnIKgJN"},{componentName:"features",componentId:"features-ncf3WpTA"},{componentName:"footer",componentId:"footer-cUSAiiVd"}];
console.log(components.filter((set => f => !set.has(f.componentId) && set.add(f.componentId))(new Set)))
|