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.

10
  • 1
    What are the examples you're referring to and why didn't they work? Commented Jun 22, 2018 at 17:29
  • Why is there no 20 in your output? Commented Jun 22, 2018 at 17:32
  • 1
    @PatrickRoberts, this is the example that I thought it was close to what I am looking for..stackoverflow.com/questions/2218999/… Commented Jun 22, 2018 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. Commented Jun 22, 2018 at 17:35
  • @MikeMcCaughan, thanks, edited the expected output... Commented Jun 22, 2018 at 17:35

4 Answers 4

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]

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

1 Comment

@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 :)
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);

3 Comments

sweet! this helped.
Glad it worked for you formosanblackbear, happy coding!
adding the information about what code is doing is often advisable.
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)

1 Comment

you may want to result.push(+JSON.parse(item).licenseNum)
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)

3 Comments

you could've used const just as another ES6 signature ;)
However the result here isn't [6, 6, 6, 20, 10, 10, 10]
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

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.