Skip to content
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

Open
xgqfrms opened this issue Jul 31, 2019 · 6 comments
Open

js & Array to Set #88

xgqfrms opened this issue Jul 31, 2019 · 6 comments
Labels
js & Array & Set js & Array & Set js & Array to Set js & Array to Set

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Jul 31, 2019

js & Array to Set

// js & Array to 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]);

https://codepen.io/xgqfrms/pen/jgmzYd


demo

let oldData = [1, 2, 3];

let newData = [3, 4, 5];

let data = oldData.concat(newData);
// (6) [1, 2, 3, 3, 4, 5]

// Array to Set
data = [...(new Set(data)];
// VM437:2 Uncaught SyntaxError: Unexpected token ]

let temp = new Set(data);
// Set(5) {1, 2, 3, 4, 5}

data = [...temp];
// (5) [1, 2, 3, 4, 5]

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

js & Array to Set

filter & unique item

https://stackoverflow.com/questions/28965112/javascript-array-to-set

Set

https://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]);

Repository owner locked and limited conversation to collaborators Jul 31, 2019
@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

image

@xgqfrms xgqfrms added js & Array & Set js & Array & Set js & Array to Set js & Array to Set labels Jul 31, 2019
@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

object bug

image

let 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 unique

https://stackoverflow.com/questions/15125920/how-to-get-distinct-values-from-an-array-of-objects-in-javascript

remove duplicate objects from an array

https://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

https://gist.github.com/telekosmos/3b62a31a5c43f40849bb

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

"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

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

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

image

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 31, 2019

"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,
};


Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
js & Array & Set js & Array & Set js & Array to Set js & Array to Set
Projects
None yet
Development

No branches or pull requests

1 participant