0

my issue is that I need to filter a set of data...

my data carries..

var arrayWithDuplicates = [
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"CA"},
    {"licenseNum": "6", "state":"CA"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "20", "state":"NV"},
    {"licenseNum": "10", "state":"CA"},
    {"licenseNum": "10", "state":"NV"},
    {"licenseNum": "10", "state":"OR"},
    {"licenseNum": "10", "state":"CA"}
];

new data should be

newdata = [6, 6, 6, 20, 10, 10, 10];

I've tried to do..

for (i = 0; i < arrayWithDuplicates .length; i++) {
                if ((arrayWithDuplicates[i].licenseNum) === -1)) {
                    newdata .push({
                        licenseNum: arrayWithDuplicates[i].licenseNum,
                        state: arrayWithDuplicates[i].state
                    });
                }
            };

the result from what these, I get..

newdata = [6, 20, 10]

I've seen a lot of great examples, but it still doesn't resolve my issue. much appreciated.

  • 1
    What are the examples you're referring to and why didn't they work? – Patrick Roberts Jun 22 '18 at 17:29
  • Why is there no 20 in your output? – Heretic Monkey Jun 22 '18 at 17:32
  • 1
    @PatrickRoberts, this is the example that I thought it was close to what I am looking for..https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript – tieguanyindev Jun 22 '18 at 17:34
  • 2
    Rather than pointing us to things you think are what you want, tell us what you want. You've given us a before and after, with no information about the parts inbetween. – Taplar Jun 22 '18 at 17:35
  • @MikeMcCaughan, thanks, edited the expected output... – tieguanyindev Jun 22 '18 at 17:35
  • in order, I think expected output you are looking for is `[ 6, 6, 6, 20, 10, 10 ]`, there should be two entries for 10, since CA is repeated thrice – kiddorails Jun 22 '18 at 17:36
  • @Taplar, i am looking for an answer in javascript that can produce the output that i am looking for... – tieguanyindev Jun 22 '18 at 17:37
  • You've given us the before and after, but have not explained the after. Why are certain things not included. "I expect 3 6's to be returned because ..." "I expect 2 10's to be returned because ...". – Taplar Jun 22 '18 at 17:38
  • @formosanblackbear No. That's now how the community helps here. You should be able to phrase what you are exactly looking for when you want to convert an input to an output; with the attempt you made to solve the problem. I'm removing my answer. – kiddorails Jun 22 '18 at 17:38
  • Okay, now why are there three 10s? If you just tell us what the criteria are, it makes it easier to answer. – Heretic Monkey Jun 22 '18 at 17:42

4 Answers4

3

Get unique entries and filter out what you need. Find comments inline below to see the approach

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

json = arrayWithDuplicates.map(x => JSON.stringify(x)) // maps each array entry in stringified json, so I could get unique entries below

var result = json.filter(function(item, pos) {
    return json.indexOf(item) == pos; // gives unique entries
}).map(x => +JSON.parse(x).licenseNum) // parses back json and gives licenseNum
console.log(result)
// [6, 6, 6, 20, 10, 10]
kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • @formosanblackbear that's great. you can accept this as answer if it helped you out. And will urge you to be more specific with your queries in future; it saves a lot of time for everyone :) – kiddorails Jun 22 '18 at 18:00
1

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

var newdata = [];
var seens = [];
for(var i = 0; i < arrayWithDuplicates.length; i++) {
  var obj = arrayWithDuplicates[i];
  var lookupKey = obj.licenseNum + "|" + obj.state;
  if(seens.indexOf(lookupKey) == -1) {
    newdata.push(obj.licenseNum);
    seens.push(lookupKey);
  }
}

console.log(newdata);
Tom G
  • 3,650
  • 1
  • 20
  • 19
1

ES6 JS:

var arr = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"}
];

var newarr = arr.
    filter((x, i) => arr.some((y, j) => {
        return y.licenseNum == x.licenseNum && i != j
    }))
    .map(x => x.licenseNum)
    console.log(newarr)
JackNavaRow
  • 194
  • 3
  • 14
  • you could've used `const` just as another ES6 signature ;) – Ivan Jun 22 '18 at 18:05
  • However the result here isn't `[6, 6, 6, 20, 10, 10, 10]` – Ivan Jun 22 '18 at 18:09
  • 1
    Try this var newarr = arr. reduce((acc, x, i) => { const gr = x.licenseNum + x.state; if ( ! acc.groups[gr]) { acc.res.push(x.licenseNum); acc.groups[gr] = 1 } return acc }, {groups: [], res: []}).res –  Jun 22 '18 at 18:18
1

a small change proposed by @kiddorails in an unique function

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

json = arrayWithDuplicates.map(x => JSON.stringify(x)) 
let result = [];
json.forEach(function(item, pos) {
    if (json.indexOf(item) == pos) {
      result.push(+JSON.parse(item).licenseNum)
    }
})
console.log(result)
JackNavaRow
  • 194
  • 3
  • 14