javascript leave max 3 occurrences of the same obj in an array

javascript array
javascript object
javascript array of objects
javascript map
javascript array methods
convert object to array of objects javascript
javascript initialize array with values
convert object to array javascript es6

If I populate an array of pairs (inside a get json) like so:

var arrItems = [];
for (let y=0; y<50 ;y++){
    var titl = data.item[y].name;
    var img = data.item[y].image[0].url;

How can I then filter it to leave only 3 pairs where value is the same?


arrItems = [


arrItems = [

Both JavaScript or jQuery are OK.

You could take a hash table and count the occurences of the wanted property and filter with a max value.

var items = [{ t: 'one', i: 'square.jpg' }, { t: 'two', i: 'square.jpg' }, { t: 'three', i: 'square.jpg' }, { t: 'four', i: 'square.jpg' }, { t: 'five', i: 'triangle.jpg' }],
    count = {},
    result = items.filter(({ i }) => {
        count[i] = (count[i] || 0) + 1;
        return count[i] <= 3;

.as-console-wrapper { max-height: 100% !important; top: 0; }

Array.from(), 问题: If I populate an array of pairs (inside a get json) like so: var arrItems = []; for ( let y=0; y<50 ;y++){ var titl = data.item[y].name; var img� JavaScript JS Array. The max() method returns the number with the highest value. Tip: The min() method returns the number with the lowest value. Browser Support.

There might be some more efficient ways to write it, but I would think the easiest to understand is the straightforward iteration with counting:

var counts = {};
for (var i = 0; i < arrItems.length; i++) {
    var item = arrItems[i];
    var count = counts[item.i] || 0;
    if (count >= 3) {
        arrItems.splice(i, 1);
    } else {
        counts[item.i] = ++count;

Data Structures: Objects and Arrays :: Eloquent JavaScript, console.log(Array.from([1, 2, 3], x => x + x));. 5 has the same result as Array. from( obj ).map( mapFn , thisArg ) , are "inherited" by subclasses of Array , and create new instances of the subclass, not Array . var toLength = function (value) { var len = toInteger(value); return Math.min(Math.max(len, 0),� Because MAX_VALUE is a static property of Number, you always use it as Number.MAX_VALUE, rather than as a property of a Number object you created. Examples Using MAX_VALUE. The following code multiplies two numeric values. If the result is less than or equal to MAX_VALUE, the func1 function is called; otherwise, the func2 function is called.

You can use reduce function

    var arrItems = [

    var output = arrItems.reduce((acc, {t,i})=>{
        acc['mem'][i] = (acc['mem'][i] || 0) + 1;
        acc['mem'][i] <= 3 ? acc['output'].push({t, i}) : ''; 
        return acc;
    }, {'mem':{}, 'output':[]});


The 10 Most Common Mistakes JavaScript Developers Make, The irregular occurrences of the transformation make him suspect that they Say , for example, that we want to represent a collection of the numbers 2, 3, 5, 7, and 11. In the second, we access the property named max in the Math object ( which is a The object1 and object2 bindings grasp the same object, which is why� JavaScript Datetime: Exercise-11 with Solution. Write a JavaScript function to get the maximum date from an array of dates. Test Data: console.log(max_date(['2015/02/01', '2015/02/02', '2015/01/03'])); Output: "2015/02/02" Sample Solution:-HTML Code:

PHP array_search() Function, If you need help figuring out why your JavaScript isn't working, consult this list of the have witnessed the proliferation of a wide array of powerful JavaScript- based to occur, so we'll just highlight a couple of their more common occurrences. important that they both get the same object, even if priorThing gets assigned� It adds each num (number) in the array to the total value. The 0 at the end of the callback indicates the initial value for total. Starting with 1, the reduce() method adds 1 to the total then moves on to the next value in the array (1 + 0). 2 is added to total (2 + 1). 3 is added to total (3 + 3), and the value of total (6) is returned

Node.js v14.6.0 Documentation, The array_search() function search an array for a value and returns the key. When set to true, the number 5 is not the same as the string 5 (See example 2)� The problem with Math.min() is it expects each parameter to be input separately and cannot take an array of multiple numbers as input. To illustrate this, take a look at the code below: Math.min(1,2,3,4) // 1 Math.min([1,2,3,4]) // NaN. As you can see, Math.min() only works when each number is input as a separate parameter.

Remove all occurrences of an element from Array in Java , Cleanup on exit of the current Node.js instance deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]); // AssertionError: Expected inputs to be strictly deep-equal: // + actual All errors thrown by the assert module will be instances of the AssertionError class. Returns: <Array> of objects containing information about the wrapper� The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.