How to count unique value from object of array in javascript
I want to calculate number of unique value and put that in new array.
I have below array:
[
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
I want new array with below result:
[
{ CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]
In this check by id, if id is same show count and new in new array.
Hope you understand what I want.
Thanks,
javascript arrays array-push
add a comment |
I want to calculate number of unique value and put that in new array.
I have below array:
[
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
I want new array with below result:
[
{ CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]
In this check by id, if id is same show count and new in new array.
Hope you understand what I want.
Thanks,
javascript arrays array-push
add a comment |
I want to calculate number of unique value and put that in new array.
I have below array:
[
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
I want new array with below result:
[
{ CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]
In this check by id, if id is same show count and new in new array.
Hope you understand what I want.
Thanks,
javascript arrays array-push
I want to calculate number of unique value and put that in new array.
I have below array:
[
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
I want new array with below result:
[
{ CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]
In this check by id, if id is same show count and new in new array.
Hope you understand what I want.
Thanks,
javascript arrays array-push
javascript arrays array-push
asked Nov 13 at 11:32
rohit13807
307213
307213
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1
var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)add a comment |
Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array
let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)add a comment |
const array_unique = require('array-hyper-unique').array_unique
let arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
array_unique(arr).length
add a comment |
You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.
yourArray.sort((a, b) => {
if (a.CategoryName > b.CategoryName) {
return 1;
}
if (a.CategoryName < b.CategoryName) {
return -1;
}
return 0;
});
var pivot = yourArray[0];
pivot.count = 0;
var counted = [pivot];
yourArray.forEach(item => {
if (item.CategoryId === pivot.CategoryId) {
pivot.count++;
} else {
pivot = item;
pivot.count = 1;
counted.push(pivot);
}
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280115%2fhow-to-count-unique-value-from-object-of-array-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1
var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)add a comment |
You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1
var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)add a comment |
You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1
var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1
var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)var arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
, { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
, { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
var tempResult = {}
for(let { CategoryColor, CategoryId } of arr)
tempResult[CategoryId] = {
CategoryId,
CategoryColor,
count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
}
let result = Object.values(tempResult)
console.log(result)edited Nov 13 at 11:47
answered Nov 13 at 11:40
Nitish Narang
2,948815
2,948815
add a comment |
add a comment |
Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array
let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)add a comment |
Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array
let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)add a comment |
Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array
let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array
let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)let k = [{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
},
{
CategoryId: "9872cce5af92",
CategoryName: "Category 2",
CategoryColor: "purple"
},
{
CategoryId: "b5c3f43f941c",
CategoryName: "Category 1",
CategoryColor: "cgreen"
}
]
let result = k.reduce(function(acc, curr) {
// Check if there exist an object in empty array whose CategoryId matches
let isElemExist = acc.findIndex(function(item) {
return item.CategoryId === curr.CategoryId;
})
if (isElemExist === -1) {
let obj = {};
obj.CategoryId = curr.CategoryId;
obj.count = 1;
obj.CategoryColor = curr.CategoryColor;
acc.push(obj)
} else {
acc[isElemExist].count += 1
}
return acc;
}, )
console.log(result)answered Nov 13 at 11:40
brk
25.4k31939
25.4k31939
add a comment |
add a comment |
const array_unique = require('array-hyper-unique').array_unique
let arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
array_unique(arr).length
add a comment |
const array_unique = require('array-hyper-unique').array_unique
let arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
array_unique(arr).length
add a comment |
const array_unique = require('array-hyper-unique').array_unique
let arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
array_unique(arr).length
const array_unique = require('array-hyper-unique').array_unique
let arr = [
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]
array_unique(arr).length
answered Nov 13 at 12:19
bluelovers
1538
1538
add a comment |
add a comment |
You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.
yourArray.sort((a, b) => {
if (a.CategoryName > b.CategoryName) {
return 1;
}
if (a.CategoryName < b.CategoryName) {
return -1;
}
return 0;
});
var pivot = yourArray[0];
pivot.count = 0;
var counted = [pivot];
yourArray.forEach(item => {
if (item.CategoryId === pivot.CategoryId) {
pivot.count++;
} else {
pivot = item;
pivot.count = 1;
counted.push(pivot);
}
});
add a comment |
You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.
yourArray.sort((a, b) => {
if (a.CategoryName > b.CategoryName) {
return 1;
}
if (a.CategoryName < b.CategoryName) {
return -1;
}
return 0;
});
var pivot = yourArray[0];
pivot.count = 0;
var counted = [pivot];
yourArray.forEach(item => {
if (item.CategoryId === pivot.CategoryId) {
pivot.count++;
} else {
pivot = item;
pivot.count = 1;
counted.push(pivot);
}
});
add a comment |
You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.
yourArray.sort((a, b) => {
if (a.CategoryName > b.CategoryName) {
return 1;
}
if (a.CategoryName < b.CategoryName) {
return -1;
}
return 0;
});
var pivot = yourArray[0];
pivot.count = 0;
var counted = [pivot];
yourArray.forEach(item => {
if (item.CategoryId === pivot.CategoryId) {
pivot.count++;
} else {
pivot = item;
pivot.count = 1;
counted.push(pivot);
}
});
You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.
yourArray.sort((a, b) => {
if (a.CategoryName > b.CategoryName) {
return 1;
}
if (a.CategoryName < b.CategoryName) {
return -1;
}
return 0;
});
var pivot = yourArray[0];
pivot.count = 0;
var counted = [pivot];
yourArray.forEach(item => {
if (item.CategoryId === pivot.CategoryId) {
pivot.count++;
} else {
pivot = item;
pivot.count = 1;
counted.push(pivot);
}
});
answered Nov 13 at 13:43
ptdien
744
744
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280115%2fhow-to-count-unique-value-from-object-of-array-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown