I have this java-script array:
var arr1 = [{id:'124',name:'qqq',type=3},
{id:'589',name:'www',type=1},
{id:'45',name:'eee',type=1},
{id:'567',name:'rrr',type=1},
{id:'124',name:'qqq',type=2},
{id:'589',name:'ddd',type=2},
{id:'45',name:'qqq',type=1},
{id:'567',name:'vvv',type=3}]
I need to count items in arr1
array that has type property 1 or 3.
How can I implement it using jQuery?
I have this java-script array:
var arr1 = [{id:'124',name:'qqq',type=3},
{id:'589',name:'www',type=1},
{id:'45',name:'eee',type=1},
{id:'567',name:'rrr',type=1},
{id:'124',name:'qqq',type=2},
{id:'589',name:'ddd',type=2},
{id:'45',name:'qqq',type=1},
{id:'567',name:'vvv',type=3}]
I need to count items in arr1
array that has type property 1 or 3.
How can I implement it using jQuery?
Share Improve this question edited May 24, 2017 at 7:24 Death-is-the-real-truth 72.3k10 gold badges57 silver badges104 bronze badges asked May 23, 2017 at 11:57 MichaelMichael 13.6k59 gold badges170 silver badges315 bronze badges 5-
2
arr1.filter(x => x.type === 1 || x.type === 3).length
– haim770 Commented May 23, 2017 at 11:58 - @haim770 answer the question so i can upvote and the OP can chose it – 404answernotfound Commented May 23, 2017 at 11:59
- I'm sure you've been on SO long enough to at least try something, there are a lot of questions pertaining to the exact question you are asking as well.... – epoch Commented May 23, 2017 at 12:01
-
invalid shorthand property
for @haim770 – Death-is-the-real-truth Commented May 23, 2017 at 12:01 - this question has lots of duplicates already, OP should remove it (and search before asking) – user5734311 Commented May 23, 2017 at 12:04
6 Answers
Reset to default 4var arr1 = [{id:'124',name:'qqq',type=3},
{id:'589',name:'www',type=1},
{id:'45',name:'eee',type=1},
{id:'567',name:'rrr',type=1},
{id:'124',name:'qqq',type=2},
{id:'589',name:'ddd',type=2},
{id:'45',name:'qqq',type=1},
{id:'567',name:'vvv',type=3}];
var count = arr.filter(item => item.type === 3 || item.type === 1).length;
You could use Array#reduce
with a check and add the value of the parison.
var array = [{ id: '124', name: 'qqq', type: 3 }, { id: '589', name: 'www', type: 1 }, { id: '45', name: 'eee', type: 1 }, { id: '567', name: 'rrr', type: 1 }, { id: '124', name: 'qqq', type: 2 }, { id: '589', name: 'ddd', type: 2 }, { id: '45', name: 'qqq', type: 1 }, { id: '567', name: 'vvv', type: 3 }],
count = array.reduce(function (r, a) {
return r + +(a.type === 1 || a.type === 3);
}, 0);
console.log(count);
var len = $.grep(arr1, (o)=> {
return o.type === 1 || o.type === 3;
}).length;
You can call the following function:
function count(arr) {
return $.grep(arr, function( n ) {
return ( (n.type === 1) || (n.type === 3) );
}).length;
}
with your array:
count(arr1);
try this code,
var arr1 =[{ id: '124', name: 'qqq', type: 3 }, { id: '589', name: 'www', type: 1 }, { id: '45', name: 'eee', type: 1 }, { id: '567', name: 'rrr', type: 1 }, { id: '124', name: 'qqq', type: 2 }, { id: '589', name: 'ddd', type: 2 }, { id: '45', name: 'qqq', type: 1 }, { id: '567', name: 'vvv', type: 3 }]
var count = 0;
arr1.forEach(function(e) {
if (e.type == 1 || e.type == 3 )
{
count++;
}
});
alert(count);
will get the count of property type 1 and 3 in Variable "count"
function btnSaveClick() {
for (let i = 0; i < vm.kanoonCourses.length; i++) {
let cnt = 0;
cnt += vm.courses.filter(c => c.KanoonCourseTitle == vm.kanoonCourses[i]).length;
if (cnt > 1) {
toastr.error("Duplicate course!");
return;
}
else {
// save data
}
}
}