tieguanyindev
tieguanyindev

Reputation: 39

how to get repeated values from an array of objects in JavaScript?

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.

Upvotes: 0

Views: 163

Answers (4)

user2693928
user2693928

Reputation:

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)

Upvotes: 1

JackNavaRow
JackNavaRow

Reputation: 195

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)

Upvotes: 1

Tom G
Tom G

Reputation: 3660

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

Upvotes: 1

kiddorails
kiddorails

Reputation: 13014

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]

Upvotes: 3

Related Questions