Raghu Venkataa
Raghu Venkataa

Reputation: 47

Limit objects in array based on property value

I have an array like the one shown below. I need to limit the number of objects in the array with ord:1 to 5. How can I to limit the number of objects inside an array based on ord property?

[{"id":"typeahead-86-4951-option-0","label":"Spigen Samsung GS5 
  Case","model":{"ord":1,"short_description":"Samsung Galaxy S5"}},
 {"id":"typeahead-86-4951-option-1","label":"Spigen iPhone 5/5s 
  Case","model":{"ord":1,"short_description":"iPhone 5 and 5s"}},
 {"id":"typeahead-86-4951-option-2","label":"Earphones","model": 
  {"ord":1,"short_description":"Buy earphones"}},
 {"id":"typeahead-86-4951-option-5","label":"Web Conferencing","model": 
  {"ord":1,"short_description":"Request"}},
 {"id":"typeahead-86-4951-option-6","label":"Dreamweaver","model": 
  {"ord":1,"short_description":null}},
 {"id":"typeahead-86-4951-option-7","label":"SSL Certification","model": 
  {"ord":1,"short_description":"Do you need to update"}},
 {"id":"typeahead-86-4951-option-8","label":"Access","model": 
  {"ord":1,"short_description":"Microsoft Access"}},
 {"id":"typeahead-86-4951-option-9","label":"Fireworks","model": 
  {"ord":1,"short_description":"Adobe Systems Fireworks"}},
 {"id":"typeahead-86-4951-option-10","label":"Spigen iPhone 6 Case","model": 
  {"ord":1,"short_description":"For iPhone 6"}},
 {"id":"typeahead-86-4951-option-11","label":"What is a cookie? 
  \t\t","model":{"ord":4,"short_description":"What is a cookie?\t\t"}},
 {"id":"typeahead-86-4951-option-12","label":"What are phishing scams and 
  how can I avoid them?\n\t\t","model":{"ord":4,"short_description":"What 
  are phishing"}},
 {"id":"typeahead-86-4951-option-13","label":"How to Deal with 
  Spam","model":{"ord":4,"short_description":"How to Deal with Spam"}},
 {"id":"typeahead-86-4951-option-14","label":"What is Spam?","model": 
  {"ord":4,"short_description":"What is Spam?}},
 {"id":"typeahead-86-4951-option-15","label":"How to set\n\t\t","model": 
  {"ord":4,"short_description":"How\n\t\t"}}

]

Upvotes: 0

Views: 436

Answers (4)

barbsan
barbsan

Reputation: 3458

You can create dictionary with ord values as keys and store there up to 5 items.
In the end just concat all arrays in dictionary.

let input = [{
      "id": "typeahead-86-4951-option-0",
      "label": "Spigen Samsung GS5 Case",
      "model": {
        "ord": 1,
        "short_description": "Samsung Galaxy S5"
      }
    }, {
      "id": "typeahead-86-4951-option-1",
      "label": "Spigen iPhone 5/5s Case",
      "model": {
        "ord": 1,
        "short_description": "iPhone 5 and 5s"
      }
    },
    {
      "id": "typeahead-86-4951-option-2",
      "label": "Earphones",
      "model": {
        "ord": 1,
        "short_description": "Buy earphones"
      }
    },
    {
      "id": "typeahead-86-4951-option-5",
      "label": "Web Conferencing",
      "model": {
        "ord": 1,
        "short_description": "Request"
      }
    },
    {
      "id": "typeahead-86-4951-option-6",
      "label": "Dreamweaver",
      "model": {
        "ord": 1,
        "short_description": null
      }
    },
    {
      "id": "typeahead-86-4951-option-7",
      "label": "SSL Certification",
      "model": {
        "ord": 1,
        "short_description": "Do you need to update"
      }
    },
    {
      "id": "typeahead-86-4951-option-8",
      "label": "Access",
      "model": {
        "ord": 1,
        "short_description": "Microsoft Access"
      }
    },
    {
      "id": "typeahead-86-4951-option-9",
      "label": "Fireworks",
      "model": {
        "ord": 1,
        "short_description": "Adobe Systems Fireworks"
      }
    },
    {
      "id": "typeahead-86-4951-option-10",
      "label": "Spigen iPhone 6 Case",
      "model": {
        "ord": 1,
        "short_description": "For iPhone 6"
      }
    },
    {
      "id": "typeahead-86-4951-option-11",
      "label": "What is a cookie?\t\t",
      "model": {
        "ord": 4,
        "short_description": "What is a cookie?\t\t"
      }
    },
    {
      "id": "typeahead-86-4951-option-12",
      "label": "What are phishing scams and how can I avoid them?\n\t\t",
      "model": {
        "ord": 4,
        "short_description": "What are phishing"
      }
    },
    {
      "id": "typeahead-86-4951-option-13",
      "label": "How to Deal with Spam",
      "model": {
        "ord": 4,
        "short_description": "How to Deal with Spam"
      }
    },
    {
      "id": "typeahead-86-4951-option-14",
      "label": "What is Spam?",
      "model": {
        "ord": 4,
        "short_description": "What is Spam?"
      }
    },
    {
      "id": "typeahead-86-4951-option-15",
      "label": "How to set\n\t\t",
      "model": {
        "ord": 4,
        "short_description": "How\n\t\t"
      }
    }
  ],
  ordObj;

ordObj = input.reduce(function(acc, el) {
  let ord = el.model.ord;
  if (!acc.hasOwnProperty(ord)) {
    acc[ord] = [];
  }
  if (acc[ord].length < 5) {
    acc[ord].push(el);
  }
  return acc;
}, {});

let result = Object.values(ordObj).reduce((acc, el) => (acc.concat(el)), []);

console.log(result);

Upvotes: 1

Tom O.
Tom O.

Reputation: 5941

You can create something similar to what I below as tracker. Use that variable to keep a count of items by ord. In my example below, I am using Array.prototype.filter on your original array of data below. Each time the callback encounters an array element with an ord property equal to five, the tracker count is incremented. If the count is less than 5 we can add it to our new array, if not, skip it.

var tracker = (function(i) {
  var c = i;
  return {
    value: () => c,
    increment: () => c += 1,
    decrement: () => c -= 1
  }
})(0);
var data = [{
  id: 'item0',
  ord: 1
}, {
  id: 'item1',
  ord: 1
}, {
  id: 'item2',
  ord: 1
}, {
  id: 'item3',
  ord: 1
}, {
  id: 'item4',
  ord: 1
}, {
  id: 'item5',
  ord: 1
}, {
  id: 'item6',
  ord: 1
}, {
  id: 'item7',
  ord: 1
}, {
  id: 'item8',
  ord: 1
}, {
  id: 'item9',
  ord: 2
}, {
  id: 'item10',
  ord: 2
}, {
  id: 'item11',
  ord: 2
}, {
  id: 'item12',
  ord: 2
}];
var limited = data.filter(el => {
  if (el.ord === 1) {
    tracker.increment();
    if (tracker.value() < 6) {
      return true;
    }
    return false;
  }
  return true;
});
console.log(limited);

Upvotes: 0

Jeremy Thille
Jeremy Thille

Reputation: 26380

You can use a small loop like this, counting how many "ord":1 you keep, and stopping after 5 :

let input = [
	{"id":"typeahead-86-4951-option-0","label":"Spigen Samsung GS5 Case","model":{"ord":1,"short_description":"Samsung Galaxy S5"}}, {"id":"typeahead-86-4951-option-1","label":"Spigen iPhone 5/5s Case","model":{"ord":1,"short_description":"iPhone 5 and 5s"}},
	{"id":"typeahead-86-4951-option-2","label":"Earphones","model": {"ord":1,"short_description":"Buy earphones"}},
	{"id":"typeahead-86-4951-option-5","label":"Web Conferencing","model": {"ord":1,"short_description":"Request"}},
	{"id":"typeahead-86-4951-option-6","label":"Dreamweaver","model": {"ord":1,"short_description":null}},
	{"id":"typeahead-86-4951-option-7","label":"SSL Certification","model":{"ord":1,"short_description":"Do you need to update"}},
	{"id":"typeahead-86-4951-option-8","label":"Access","model": {"ord":1,"short_description":"Microsoft Access"}},
	{"id":"typeahead-86-4951-option-9","label":"Fireworks","model": {"ord":1,"short_description":"Adobe Systems Fireworks"}},
	{"id":"typeahead-86-4951-option-10","label":"Spigen iPhone 6 Case","model": {"ord":1,"short_description":"For iPhone 6"}},
	{"id":"typeahead-86-4951-option-11","label":"What is a cookie?\t\t","model":{"ord":4,"short_description":"What is a cookie?\t\t"}},
	{"id":"typeahead-86-4951-option-12","label":"What are phishing scams and how can I avoid them?\n\t\t","model": {"ord":4,"short_description":"What are phishing"}},
	{"id":"typeahead-86-4951-option-13","label":"How to Deal with Spam","model":{"ord":4,"short_description":"How to Deal with Spam"}},
	{"id":"typeahead-86-4951-option-14","label":"What is Spam?","model": {"ord":4,"short_description":"What is Spam?"}},
	{"id":"typeahead-86-4951-option-15","label":"How to set\n\t\t","model": {"ord":4,"short_description":"How\n\t\t"}}
],
	output = [],
	count = 0;
   
input.forEach( obj => {
  if(obj.model.ord===1){
    if(count>=5) return
    count++
  }
 output.push(obj)
})

console.log("Count before : " + input.filter(o=>o.model.ord===1).length )
console.log("Count after : " + output.filter(o=>o.model.ord===1).length )

Upvotes: 1

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, use filter methos to filter out order:1 records and slice array methods to get first 5

let final = arr.filter(v => v.model.ord === 1).slice(0, 5);

codepen - https://codepen.io/nagasai/pen/NOmXar?editors=1010

let arr = [{"id":"typeahead-86-4951-option-0","label":"Spigen Samsung GS5 Case","model":{"ord":1,"short_description":"Samsung Galaxy S5"}},
 {"id":"typeahead-86-4951-option-1","label":"Spigen iPhone 5/5s Case","model":{"ord":1,"short_description":"iPhone 5 and 5s"}},
 {"id":"typeahead-86-4951-option-2","label":"Earphones","model": 
  {"ord":1,"short_description":"Buy earphones"}},
 {"id":"typeahead-86-4951-option-5","label":"Web Conferencing","model": 
  {"ord":1,"short_description":"Request"}},
 {"id":"typeahead-86-4951-option-6","label":"Dreamweaver","model": 
  {"ord":1,"short_description":null}},
 {"id":"typeahead-86-4951-option-7","label":"SSL Certification","model": 
  {"ord":1,"short_description":"Do you need to update"}},
 {"id":"typeahead-86-4951-option-8","label":"Access","model": 
  {"ord":1,"short_description":"Microsoft Access"}},
 {"id":"typeahead-86-4951-option-9","label":"Fireworks","model": 
  {"ord":1,"short_description":"Adobe Systems Fireworks"}},
 {"id":"typeahead-86-4951-option-10","label":"Spigen iPhone 6 Case","model": 
  {"ord":1,"short_description":"For iPhone 6"}},
 {"id":"typeahead-86-4951-option-11","label":"What is a cookie? \t\t","model":{"ord":4,"short_description":"What is a cookie?\t\t"}},
 {"id":"typeahead-86-4951-option-12","label":"What are phishing scams and how can I avoid them?\n\t\t","model":{"ord":4,"short_description":"What are phishing"}},
 {"id":"typeahead-86-4951-option-13","label":"How to Deal with Spam","model":{"ord":4,"short_description":"How to Deal with Spam"}},
 {"id":"typeahead-86-4951-option-14","label":"What is Spam?","model": {"ord":4,"short_description":"What is Spam?"}},
 {"id":"typeahead-86-4951-option-15","label":"How to set\n\t\t","model": {"ord":4,"short_description":"How\n\t\t"}}]
  

let final = arr.filter(v => v.model.ord === 1).slice(0, 5);

console.log(final)

Upvotes: 0

Related Questions