0
var names = [{id:1,name:"name1"}, {id:2,name:"name2"}, {id:3,name:"name1"}, {id:4,name:"name1"}, {id:5,name:"name2"}, {id:6,name:"name3"}]

This is my array of objects in javascript - I am looking for a way to pull out all duplicates, and individuals that have the same name property - and place them into an object of arrays.. Filtering out the duplicate objects is my primary concern. If possible to do this in jQuery that is fine.

{
    [{id:1,name:"name1"},{id:3,name:"name1"}{id:4,name:"name1"}]
    [{id:2,name:"name2"},{id:5,name:"name2"}] 
    [{id:6,name:"name3"}]
}

3 Answers 3

1

You could do something like this:

var names = [{id:1,name:"name1"}, {id:2,name:"name2"}, {id:3,name:"name1"}, {id:4,name:"name1"}, {id:5,name:"name2"}, {id:6,name:"name3"}]

var sorted = {};

for (var i=0; i < names.length; i++) {
    if (!sorted[names[i].name]) {
        sorted[names[i].name] = [];
    }
    sorted[names[i].name].push(names[i]);
}

Here's a fiddle.

If you want, for example, the array of all items with name == "name1", you just do:

var allName1s = sorted["name1"];
Sign up to request clarification or add additional context in comments.

5 Comments

Kind of peculiar to use an array for sorted when you're not using it as an array. A plain object would be better fit.
@RoyJ: I don't disagree. I originally had it as a plain object, but it really doesn't matter. You can use non-numeric indexes for an array. I kept it as an array because that's what the OP said they wanted.
Great work @MattBurland. I think this is close to exactly what I am looking for.
@RoyJ: On further consideration, I think I like it better with a plain object, so I switched it back. Non-numeric array indexes are kind of a pain for properties like length.
Thanks for the input on that @RoyJ - I think I agree that having this as an object would make more sense rather then having it as an array.
1

The approach Matt Burland gives is the standard way of finding duplicates. I use it here, but at the same time build the array of arrays you were looking for.

var names = [{id:1,name:"name1"}, {id:2,name:"name2"}, {id:3,name:"name1"}, {id:4,name:"name1"}, {id:5,name:"name2"}, {id:6,name:"name3"}]

var collector = {};
var sorted = [];
for (var i = 0; i < names.length; ++i) {
    var entry = names[i];
    if (!(entry.name in collector)) {
        collector[entry.name] = [];
        sorted.push(collector[entry.name]);
    }
    collector[entry.name].push(entry);
}
console.dir(collector);
console.dir(sorted);

The requisite Fiddle

1 Comment

Thanks @RoyJ this looks like a appropriate approach at well to accomplish my needs.
0

If you want to sort the data into valid and duplicate, i would do it like this:

Javascript:

<script type="text/javascript">

var names = [{id:1,name:"name1"}, {id:2,name:"name2"}, {id:3,name:"name1"}, {id:4,name:"name1"}, {id:5,name:"name2"}, {id:6,name:"name3"}]

var parsed_names = parseNames(names);

console.log(parsed_names);

function parseNames(names) 
{
    var parsed_data = [];
    var duplicate_data = [];
    var name_list = [];
    for (var i = 0; i < names.length; i++) {
        if (!inArray(names[i].name, name_list)) {
            parsed_data.push(names[i]);
        } else {
            duplicate_data.push(names[i]);
        }
        name_list.push(names[i].name);
    }
    return {
        'ParsedData': parsed_data,
        'DuplicateData': duplicate_data
    };
}

function inArray(needle, haystack) {
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] === needle) {
            return true;
        }
    }
    return false;
}

</script>

Console output (debug):

enter image description here

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.