-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
js & Array to Set #88
Comments
js & Array to Set
https://stackoverflow.com/questions/28965112/javascript-array-to-set Sethttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set let arr = [1, 2, 3, 3, 4, 5];
console.log(`arr =`, arr);
const set = new Set(arr);
console.log(`set =`, set);
console.log(`[...set] =`, [...set]);
|
object buglet newData = [{K: 3}, {K: 4}, {K: 5}];
// (3) [{…}, {…}, {…}]
let oldData = [{K: 3}, {K: 2}, {K: 1}];
// (3) [{…}, {…}, {…}]
data = oldData.concat(newData);
// (6) [{…}, {…}, {…}, {…}, {…}, {…}]0: {K: 3}1: {K: 2}2: {K: 1}3: {K: 3}4: {K: 4}5: {K: 5}length: 6__proto__: Array(0)
temp = new Set(data);
// Set(6) {{…}, {…}, {…}, {…}, {…}, …}size: (...)__proto__: Set[[Entries]]: Array(6)0: Objectvalue: {K: 3}1: value: {K: 2}2: value: {K: 1}3: Object4: Object5: Objectlength: 6
data = [...temp];
(6) [{…}, {…}, {…}, {…}, {…}, {…}] object array filter uniqueremove duplicate objects from an arrayhttps://reactgo.com/removeduplicateobjects/ https://dev.to/saigowthamr/how-to-remove-duplicate-objects-from-an-array-javascript-48ok https://dev.to/vuevixens/removing-duplicates-in-an-array-of-objects-in-js-with-sets-3fep |
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description getUniqueObjects
* @augments
* @example
* @link
*
*/
const getUniqueObjects = (arr = [], key = ``, debug = false) => {
let result = [];
result = arr.map(e => e[key])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e]).map(e => arr[e]);
return result;
};
export default getUniqueObjects;
export {
getUniqueObjects,
};
/*
function getUnique(arr, comp) {
const unique = arr
.map(e => e[comp])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e]).map(e => arr[e]);
return unique;
}
*/
https://dev.to/saigowthamr/how-to-remove-duplicate-objects-from-an-array-javascript-48ok |
OK"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description getUniqueObjects
* @augments
* @example
* @link
*
*/
const getUniqueObjects = (arr = [], key = ``, debug = false) => {
let log = console.log;
let result = [];
result = arr.map(e => e[key])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e]).map(e => arr[e]);
if (debug) {
log(`arr =`, JSON.stringify(arr, null, 4));
log(`result =`, JSON.stringify(result, null, 4));
} else {
log(`arr.length =`, arr.length);
log(`result.length =`, result.length);
}
return result;
};
export default getUniqueObjects;
export {
getUniqueObjects,
};
|
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description getUniqueObjects
* @augments
* @example
* @link
*
*/
const getUniqueObjects = (arr = [], key = ``, debug = false) => {
let log = console.log;
let result = [];
result = arr.map(e => e[key])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter(e => arr[e])
.map(e => arr[e]);
if (debug) {
log(`arr =`, JSON.stringify(arr, null, 4));
log(`result =`, JSON.stringify(result, null, 4));
} else {
log(`arr.length =`, arr.length);
log(`result.length =`, result.length);
}
return result;
};
export default getUniqueObjects;
export {
getUniqueObjects,
};
|
js & Array to Set
https://codepen.io/xgqfrms/pen/jgmzYd
demo
The text was updated successfully, but these errors were encountered: