4

This is the current JSON file:

[{
    "name": "Peter",
    "age": 30,
    "hair color": "brown"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}]

I want to remove the duplicate Steve individual from the list. How can I make a new JSON that checks if the object's name matches and remove any duplicates in JavaScript?

5
  • Does the order matter? Commented Mar 24, 2014 at 15:56
  • 1
    So you only care about the name, and none of the other properties? Commented Mar 24, 2014 at 15:58
  • Order does not matter. And we only care if 'name' matches. If there are two of the same, just have one remaining. Commented Mar 24, 2014 at 16:06
  • @user2836857 And what if other details in the objects are different? Which one to keep? Commented Mar 24, 2014 at 16:07
  • Good question. I am assuming they are all exact but you bring a good point up. Commented Mar 24, 2014 at 16:14

6 Answers 6

10

You must load the JSON data in to the program and parse that with JSON.parse, like this

var array = JSON.parse(content.toString())

To filter out the repeated names from the array of Objects, we use Array.prototype.filter function. You can store the names in an object, and next time when the same name appears we simply filter it out from the result.

var seenNames = {};

array = array.filter(function(currentObject) {
    if (currentObject.name in seenNames) {
        return false;
    } else {
        seenNames[currentObject.name] = true;
        return true;
    }
});

console.log(array);
# [ { name: 'Peter', age: 30, 'hair color': 'brown' },
#   { name: 'Steve', age: 55, 'hair color': 'blonde' } ]
Sign up to request clarification or add additional context in comments.

Comments

5

var data = [{
    "name": "Peter",
    "age": 30,
    "hair color": "brown"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}]
 data = this.data.filter((obj, pos, arr) => {
            return arr.map(mapObj =>
                  mapObj.name).indexOf(obj.name) == pos;
            });
          console.log(data);

Comments

2

Using Underscore.js and the uniq function:

_.uniq(array, false, function (item) { return item.name; })

5 Comments

No downvote but I don't think OP mentioned anything about underscore.
@thefourtheye I'm well aware. The answer isn't wrong, it just suggests using a JS tool.
It's irrelevant if OP doesn't want to include a whole library just for this one task. You should make that determination first.
@cookiemonster I just am not a fan of reinventing the wheel every time a simple problem presents itself. Yes, the code I gave suggests using a library, and certainly people often do not want to add another library to the mix. However, it is extremely declarative code. thefourtheye's answer, for example, is not nearly as concise and immediately understandable. To each his own I guess.
@TimothyShields Even I like using underscore :)
1

Loop, check, splice, repeat:

var distinctValues = {};
for (var i = 0; i < data.length; i++) {
    if (distinctValues.hasOwnProperty(data[i].name]) {
        //already has it
        data.splice(i, 1);
        i--;
    } else {
        distinctValues[data[i].name] = true;
    }
}

2 Comments

You'll need to decrement i when doing a .splice(), or iterate in reverse.
@cookiemonster -- Damnit, I always forget that... :\
0

This was the best solution I could find that makes you able to filter on multiple values in you json object. Solution without _ (module)

array.filter((thing, index, self) =>
    index === self.findIndex((t) => (
    t.place === thing.place && t.name === thing.name // you can add more arguments here to filter more
  ))
)

//Other example
array.filter((thing, index, self) =>
    index === self.findIndex((t) => (
    t.place === thing.place && t.name === thing.name && t.time === thing.time
  ))
)

Comments

-3

Python one-liner from CLI:

cat file_with_duplicates.json | python2 -c 'import sys; import json; sys.stdout.write(json.dumps(reduce(lambda x, y: x + [y] if y not in x else x, json.loads(sys.stdin.read()), [])))' > unique_items.txt

Comments

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.