0

I am working on some code I am unfamiliar with.

I have a data set that looks like this:

[
 {"id": 1, "person_name": "joe", "parent_id": "1ax"}
 {"id": 2, "person_name": "jane", "parent_id": "5t6"}
 {"id": 3, "person_name": "john", "parent_id": "a88"}
]

Originally the code has this line:

const values = this.data.map((d) => d.person_name).sort();

and then this:

groups = Object.values(
            values.reduce((a, c) => {
                (a[c] || (a[c] = { name: c, rcount: 0 })).rcount += 1;
                return a;
            }, {})
        ).sort(({ rcount: ac }, { rcount: bc }) => bc - ac);

This all results in something like this:

[
 {name: "joe", rcount: 1},
 {name: "jane", rcount: 1},
 {name: "john", rcount: 1}
]

I'd like to include the parent_id in the final output too but can't figure out how.

I've tried a number of things in the .map() section but none have yet worked.

I tried this for example:

const values = this.data.map((d) => d.person_name, d.parent_id).sort();

Is there a way I can map to an object and then reference the person_name and the parent_id from values when doing the reduce?

1
  • 1
    Note that the code is counting unique instances of person_name in the list. If you add parent_id, you may get different values if a person_name exists twice in the list (in which case it would have shown up as { name: "name", rcount: 2 }), but with different parent_ids (in which case it may show up as { name: "name", parent_id: "1ax", rcount: 1 }, { name: "name", parent_id: 5t6, rcount: 1 })... Commented Aug 7, 2020 at 12:21

2 Answers 2

1

Does this solves the answer ?

const data = [{
    "id": 1,
    "person_name": "joe",
    "parent_id": "1ax"
}, {
    "id": 2,
    "person_name": "jane",
    "parent_id": "5t6"
}, {
    "id": 3,
    "person_name": "john",
    "parent_id": "a88"
}];

/* OLD */
// const values = this.data.map((d) => d.person_name).sort();
// looks like ['joe', 'jane', ...string]

/* NEW */
const values = data.map((d) => ({ name: d.person_name, parent_id: d.parent_id })).sort();
// looks like [{ name : 'joe', parent_id : '1ax' }, { name : 'jane', parent_id : '5t6' }, ...object]

// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/reduce
const groups = Object.values(
    values.reduce((a, c) => {
    // the parameter 'a' is the accumulator
    // the parameter 'c' is the current element in array
    // since the array does longer contains strings but objects and we need a string as array index, we use the object name property like 'object.property' => 'c.name'
        (a[c.name] || (a[c.name] = {
            name: c.name,
            rcount: 0,
            // we add the property parent_id
            parent_id: c.parent_id
        })).rcount += 1;
        return a;
    }, {})
).sort(({
    rcount: ac
}, {
    rcount: bc
}) => bc - ac);

console.log(groups);

Sign up to request clarification or add additional context in comments.

6 Comments

Answers which explain code are voted up more often than down...
I'll edit my answer and explain it, I just wanted to be sure that it solves the problem
I added comments, let me know if it's clear or isn't
very clear, thanks! I will now incorporate what @HereticMonkey said about the count too
You can use c.name + c.parent_id as a key, instead of c.name, it will probably solves this problem. With that, only same name and same parent_id will increments rcount.
|
1

Map the 2 items you want to show to an array:

data = [
    { "id": 1, "person_name": "joe", "parent_id": "1ax" },
    { "id": 2, "person_name": "jane", "parent_id": "5t6" },
    { "id": 3, "person_name": "john", "parent_id": "a88" }
]
const values = data.map((d) => [d.parent_id,d.person_name]).sort();
groups = Object.values(
    values.reduce((a, c) => {
        (a[c] || (a[c] = { name: c[1], id:c[0], rcount: 0 })).rcount += 1;
        return a;
    }, {})
).sort(({ rcount: ac }, { rcount: bc }) => bc - ac);

console.log(groups)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.