从javascript对象列表中查找最多前两次出现的元素

时间:2019-03-22 07:11:23

标签: javascript arrays

我有一个包含以下元素的javascript数组列表

list = [
 {header: "header1", code: ""},
 {label: "label1", price: 10},
 {header: "header2", code: ""},
 {header: "header3", code: ""},
 {header: "header4", code: ""}
]

如何过滤数组以获取元素“ header”的前两次出现

预期输出:

list = [
 {header: "header1", code: ""},
 {label: "label1", price: 10},
 {header: "header2", code: ""}
]

在javascript中实现此目标的任何可行,有效的解决方案。

9 个答案:

答案 0 :(得分:2)

您可以通过检查count来过滤数组,并获取非标头项目或减少计数。

const
    topHeaders = (array, count = 2) =>
        array.filter(({ header }) => count && (!header || count--));

console.log(topHeaders([]));
console.log(topHeaders([{ header: "header1" }]));
console.log(topHeaders([{ header: "header1" }, { label: "label1" }]));
console.log(topHeaders([{ header: "header1" }, { label: "label1" }, { header: "header2" }]));
console.log(topHeaders([{ header: "header1" }, { header: "header2" }]));
console.log(topHeaders([{ header: "header1" }, { header: "header2" }, { header: "header3" }, { header: "header4" }]));
console.log(topHeaders([{ header: "header1" }, { header: "header2" }, { header: "header3" }, { label: "label1" }, { header: "header4" }]));
console.log(topHeaders([{ header: "header1" }, { label: "label1" }, { header: "header2" }, { header: "header3" }, { header: "header4" }]));
console.log(topHeaders([{ header: "header1" }, { label: "label1" }, { label: "label2" }, { header: "header2" }, { header: "header3" }, { header: "header4" }]));
console.log(topHeaders([{ header: "header1" }, { label: "label1" }, { label: "label2" }, { header: "header2" }, { header: "header3" }, { label: "label" }, { header: "header4" }]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

这是您的解决方案。

var j = 0;
var srch = 'header';
var newlist = [];
for(var i=0;i<list.length;i++)
{
    var obj = list[i];
    for(var prop in obj)
    {
        if(prop == srch)
        {
            newlist.push(obj);
            j++;
        }
    }
    if(j == 2)
    {
        break;
    }
}

您的预期输出将在新列表中。

答案 2 :(得分:0)

您可以按照以下步骤进行操作:

  1. 使用filter()找出属性为header的所有元素
  2. findIndex()上使用list来匹配数组的必需元素2
  3. 通过将使用slice()找到的索引传递到first,使用findIndex将数组的一部分从slice()标头移到所需的标头

const list = [
 {header: "header1", code: ""},
 {label: "label1", price: 10},
 {header: "header2", code: ""},
 {header: "header3", code: ""},
 {header: "header4", code: ""}
]

const getItems = (arr,num) => list.slice(0,arr.findIndex(a => a === arr.filter(x => x.hasOwnProperty('header'))[num-1])+1)

console.log(getItems(list,2));
console.log(getItems([ {header: "header1", code: ""}, {header: "header2", code: ""}],2));

答案 3 :(得分:0)

这里是您的代码。

 var list = [{header: "header1", code: ""}, {label: "label1", price: 10}, {header: "header2", code: ""}, {header: "header3", code: ""}, {header: "header4", code: ""}];

     var j = 0;
     list = list.filter((ar, i) => {
	 if(ar.hasOwnProperty('header') && j < 2){
		j++;       
		return ar;
     }
     });

     console.log(list);

答案 4 :(得分:0)

只需过滤并像下面一样取前两个。

const  list = [
 {header: "header1", code: ""},
 {label: "label1", price: 10},
 {header: "header2", code: ""},
 {header: "header3", code: ""},
 {header: "header4", code: ""}
];

const headersArray = list.filter((item) =>
  item.header
).slice(0, 2);


console.log("The results are ", headersArray)

答案 5 :(得分:0)

在这里,我将对象逐个推入另一个数组,同时计算具有header的对象。如果header等于2,则停止推动对象。

const list = [
 {header: "header1", code: ""},
 {label: "label1", price: 10},
 {header: "header2", code: ""},
 {header: "header3", code: ""},
 {header: "header4", code: ""}
], maxHeader = 2
let result = [], headCounter = 0
list.forEach(item => {
  if (headCounter < maxHeader){
    if (item.header) headCounter++
    result.push(item)
  }
})
console.log(result)

答案 6 :(得分:0)

使用Reduce:

list.reduce(function (accumulator, currentValue) {

if(accumulator.length < 2 && currentValue.hasOwnProperty('header')){
  accumulator.push(currentValue);
}
 return accumulator;
}, []);

答案 7 :(得分:0)

以下代码适用于您的情况。

var list = [
          {header: "header1", code: ""},
          {label: "label1", price: 10},
          {header: "header2", code: ""},
          {header: "header3", code: ""},
          {header: "header4", code: ""}
         ]
var counter = 0
var i = 0

list.map(obj => {
        if(counter >= 2){
           list.splice(i,i)
        }
        if(obj["header"]){
          counter++;
       }
    i++;
})

答案 8 :(得分:0)

var arr = [];
var count = 0;
for(var i = 0, j = list.length; i < j; i++) {
    if(count >=2){
        break
    }
    list[i]['header'] ? count++ : '';
    arr.push(list[i]);
}

console.log(arr);

希望它对您有帮助!