Reputation: 294
I have an array :
let array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'];
How can I filter this array if in result I need it:
let filterArray = [['yay', 3] ['opps', 1], ['yay', 2], ['opps' 3]];
Thank you.
Upvotes: 1
Views: 6306
Reputation: 2140
You can use this:
let filterArray = array.reduce((acc, item) => {
let similarItemIndex = acc.findIndex(i => i[0] === item);
if (similarItemIndex > -1) {
acc.push([item, 1]);
} else {
acc[similarItemIndex] = [item, acc[similarItemIndex][0] + 1];
}
return acc;
}, []);
Upvotes: 0
Reputation: 68393
Use reduce
arr.reduce( (a,c) => {
var lastValue = (a[a.length - 1] || [])[0]; //find last item in accumulator a
c == lastValue ? a[a.length - 1][1]++ : a.push([c,1]); //if last value is same then increase count, else add new item
return a;
} ,[]) //initialize accumulator a to empty array
Demo
var arr = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'];
var output = arr.reduce( (a,c) => {
var lastValue = (a[a.length - 1] || [])[0];
c == lastValue ? a[a.length - 1][1]++ : a.push([c,1]);
return a;
} ,[]);
console.log( output );
Upvotes: 2
Reputation: 334
Yet another solution:
let array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'];
let rs = {};
array.forEach((w) => rs[w] == undefined ? rs[w] = 1 : rs[w] += 1);
let result = Object.keys(rs).map((key) => [key, rs[key]]);
console.log(result);
Upvotes: 0
Reputation: 22534
You can use array#reduce
to group similar words in an array and for repeating word increment its count.
let array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'],
result = array.reduce((r,word,i,a) => {
word == a[i-1] ? r[r.length - 1][1]++ : r.push([word, 1]);
return r;
},[]);
console.log(result);
Upvotes: 0
Reputation: 2753
You can use Array.reduce
method to iterate through your original array and create the new array.
acc
(accumulated array) is empty push the [curr, 1]
(current variable and count) in the arrayacc
(accumulated array) last pushed index contains the curr
variable, then update the countacc
(accumulated array) last pushed index does not contains the curr
variable, then push the [curr, 1]
in acc
.let array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'];
let modifiedArray = array.reduce((acc, curr) => {
if (!acc.length) {
acc.push([curr, 1]);
} else {
let lastPushedArray = acc[acc.length-1];
if (lastPushedArray[0] === curr) {
acc[acc.length-1][1]++;
} else {
acc.push([curr, 1]);
}
}
return acc;
}, []);
console.log(modifiedArray);
Upvotes: 5
Reputation: 386578
You could reduce the array by checking the last element of the temporary result set.
var array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'],
result = array.reduce((r, v) => {
var last = r[r.length - 1];
if (last && last[0] === v) {
last[1]++;
} else {
r.push([v, 1]);
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 3366
Try the below code. This will not give you the exact output. But you can follow the below method to generate desired output.
var array = ['yay', 'yay', 'yay', 'opps', 'yay', 'yay', 'opps', 'opps', 'opps'];
var newArray = {};
array.map( function (a) { if (a in newArray) newArray[a] ++; else newArray[a] = 1; } );
console.log(newArray);
Output
{ yay: 5, opps: 3 }
Upvotes: 0