根据属性值限制数组中的对象

时间:2018-10-29 14:04:01

标签: javascript arrays

我有一个如下所示的数组。我需要将ord:1数组中的对象数限制为5。如何基于ord属性来限制数组中的对象数?

[{"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"}}

]

4 个答案:

答案 0 :(得分:1)

您可以使用这样的小循环,计算保留的"ord":1数,并在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 )

答案 1 :(得分:1)

您可以使用ord值作为键来创建字典,并在其中最多存储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"
      }
    }
  ],
  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);

答案 2 :(得分:0)

要获得预期结果,请使用过滤方法过滤掉order:1条记录,并使用切片数组方法获得前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)

答案 3 :(得分:0)

您可以创建类似于以下我称为tracker的内容。使用该变量可按ord保留项目计数。在下面的示例中,我在下面的原始数据数组上使用Array.prototype.filter。每次回调遇到ord属性等于5的数组元素时,tracker计数都会增加。如果计数少于5,则可以将其添加到新数组中,否则请跳过该计数。

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);