过滤数组并重置索引

时间:2018-07-22 03:54:13

标签: javascript

我有一个这样的对象数组:

const data = [
  {
    name: "Peter",
    age: 20,
    nationality: "American", 
    index: 0
  },
  {
    name: "David",
    age: 25,
    nationality: "English", 
    index: 1
  },
  {
    name: "Gabriel",
    age: 23,
    nationality: "Spanish", 
    index: 2
  },
  {
    name: "Kate",
    age: 22,
    nationality: "English", 
    index: 3
  },
];

如果我只想返回只包含英语国籍的人的新数组,则可以使用filter,如下所示:

let englishPerson = data.filter(el => el.nationality === 'English');
console.log(englishPerson);

这将记录以下内容:

> Array [Object { name: "David", age: 25, nationality: "English", index: 1 }, Object { name: "Kate", age: 22, nationality: "English", index: 3 }]

但是我想在过滤数据后重置索引,因此新过滤后的数组中的第一个对象的索引应为0,第二个索引应为1,依此类推。在这种情况下,David的索引为1,因为它与原始数据保持相同的索引。

5 个答案:

答案 0 :(得分:3)

您可以在filter之后加上map,但最好与reduce一次性完成-如果该项目通过测试,则将其添加到累加器中,累加器当前长度的index

const data=[{name:"Peter",age:20,nationality:"American",index:0},{name:"David",age:25,nationality:"English",index:1},{name:"Gabriel",age:23,nationality:"Spanish",index:2},{name:"Kate",age:22,nationality:"English",index:3},]

console.log(
  data.reduce((a, item) => {
    if (item.nationality === 'English') {
      a.push({
        ...item,
        index: a.length
      });
    }
    return a;
  }, [])
);

答案 1 :(得分:1)

您可以使用Array.prototype.map()来修改过滤结果的index属性:

const data = [
  {
    name: "Peter",
    age: 20,
    nationality: "American", 
    index: 0
  },
  {
    name: "David",
    age: 25,
    nationality: "English",
    index: 1
  },
  {
    name: "Gabriel",
    age: 23,
    nationality: "Spanish", 
    index: 2
  },
  {
    name: "Kate",
    age: 22,
    nationality: "English", 
    index: 3
  },
];

let i=0;
let englishPerson = data.filter(el => el.nationality === 'English').map(el => {
  el.index = i; i++;
  return el;
});
console.log(englishPerson);

根据您的评论更新答案:

const data = [
  {
    name: "Peter",
    age: 20,
    nationality: "American", 
    index: 0
  },
  {
    name: "David",
    age: 25,
    nationality: "English",
    index: 1
  },
  {
    name: "Gabriel",
    age: 23,
    nationality: "Spanish", 
    index: 2
  },
  {
    name: "Kate",
    age: 22,
    nationality: "English", 
    index: 3
  },
];

let i=0;
let englishPerson = data.filter(el => el.nationality === 'English').map(el => {
  if(el.hasOwnProperty('index')){
    el.index = i; i++;
  }
  return el;
});
console.log(englishPerson);

答案 2 :(得分:0)

如果您想使用您的解决方案,只需稍作改动,就可以尝试以下操作:

var c=0;
var englishPerson=data.filter(function(el){
    return el.nationality=="English" && (el.index=c++)>-1;
});
console.log(englishPerson);

答案 3 :(得分:0)

您可以使用filters.tags.data.map(f => f.Key).toString()中的替换过滤器。 在地图上您使用的地方,该索引已重置。

例如:

  $scope.ret = function(){
  data: url,
  method: post,
  retuen filters.tags.data.map(f => f.Key).toString();
 }

答案 4 :(得分:0)

执行以下操作:

const data = [
  {
    name: "Peter",
    age: 20,
    nationality: "American", 
    index: 0
  },
  {
    name: "David",
    age: 25,
    nationality: "English", 
    index: 1
  },
  {
    name: "Gabriel",
    age: 23,
    nationality: "Spanish", 
    index: 2
  },
  {
    name: "Kate",
    age: 22,
    nationality: "English", 
    index: 3
  },
];

data.filter(el => Object.values(el).includes('English')).forEach((obj, i) => {obj.index = i; console.log(obj)})

如果您想使用map,只需将forEach替换为map