0

Just getting started looking over lodash and stuck trying to figure out how to remove duplicates from arrays within nested objects.

Working with loop:

const _ = require('lodash');

let data = {
    "deviceA": {
        "serviceA": [
            "Foo",
            "Bar",
            "Foo Bar A",
            "Foo Bar A",
            "Foo Bar A"
        ],
        "serviceB": [
            "Foo",
            "Bar",
            "Foo Bar B",
            "Foo Bar B",
            "Foo Bar B"
        ]
    }
}


for (const key in data) {
    for (const key2 in data[key]) {
        data[key][key2] = _.uniqWith(data[key][key2], _.isEqual)
    }
}

console.log(data)

Is it possible using purely lodash to parse a nested object and update an array to remove duplicates without using a for loop?

2 Answers 2

2

You can combine uniq and mapValues to achieve it.

let data = {
    "deviceA": {
        "serviceA": [
            "Foo",
            "Bar",
            "Foo Bar A",
            "Foo Bar A",
            "Foo Bar A"
        ],
        "serviceB": [
            "Foo",
            "Bar",
            "Foo Bar B",
            "Foo Bar B",
            "Foo Bar B"
        ]
    },
    "deviceB": {
        "serviceA": [
            "A",
            "A",
            "Foo Bar A",
        ],
        "serviceB": [
            "B",
            "B",
            "Foo Bar B",
        ]
    }
}

const items = _.mapValues(data, (device) => _.mapValues(device, (service) =>_.uniq(service)))
console.log(items)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

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

Comments

1

I think you can consider a solution without lodash, below is an example of how this can be implemented natively using pure js.

let data = {
    "deviceA": {
        "serviceA": [
            "Foo",
            "Bar",
            "Foo Bar A",
            "Foo Bar A",
            "Foo Bar A"
        ],
        "serviceB": [
            "Foo",
            "Bar",
            "Foo Bar B",
            "Foo Bar B",
            "Foo Bar B"
        ]
    }
}

const noDup = Object.entries(data).reduce((p, n) => {
  const a = Object.entries(n[1]).reduce((p1, n1) => 
    ({...p1, [n1[0]]: [...new Set(n1[1])]})
  , {})
  return {...p, [n[0]]: a}
}, {})

console.log(noDup)

3 Comments

Accepted the lodash specific one for the use case, but like this pure implementation as well.
Thats okay, Though consider avoiding lodash, You should aim at making your project as small as possible and if a library can be avoided then avoid it
Yes I saw a lot of question that solve by native javascript. That's is really awesome. But I think that is a trade off between the stable of the code, readable, support functions of lodash with source code size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.