将元素与另一个数组匹配的数组过滤器对象(具有键“ id”)

时间:2019-01-24 10:39:29

标签: javascript

我有一个数组对象,每个对象都有一个键“ id”,还有另一个数组whoes元素实际上是第一个数组的id,所以我想从第一个数组中过滤出对象,而ids与第二个数组匹配数组

const categories = [ 
  { id: 1,
    name: 'Permaculture',
    active: false,
    createdAt: '2018-06-18T11:38:41.000Z',
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 2,
    name: 'Food Forest',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 3,
    name: 'Community Supported Farming (CSA)',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 4,
    name: 'Urban Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 5,
    name: 'Roof Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 6,
    name: 'Roof Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 7,
    name: 'Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 8,
    name: 'Indoor Plantation',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 9,
    name: 'Soil Culture',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 10,
    name: 'Tropical Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 11,
    name: 'Greenhouse',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 12,
    name: 'Hobby Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 13,
    name: 'WWOOF',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z" },
  { id: 14,
    name: 'Organic Farming',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z" } 
];

const ids = [1, 4, 2];

const expected_res =    [
    {
        id: 1,
        name: "Permaculture" 
    },
    {
        id: 4,
        name: "Urban Farming"
    },
    {
        id: 2,
        name: "Food Forest"
    }
]

3 个答案:

答案 0 :(得分:0)

这将根据您的条件进行过滤,但不会保留ID的顺序。

const categories = [{
    id: 1,
    name: 'Permaculture',
    active: false,
    createdAt: '2018-06-18T11:38:41.000Z',
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 2,
    name: 'Food Forest',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 3,
    name: 'Community Supported Farming (CSA)',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 4,
    name: 'Urban Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 5,
    name: 'Roof Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 6,
    name: 'Roof Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 7,
    name: 'Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 8,
    name: 'Indoor Plantation',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 9,
    name: 'Soil Culture',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 10,
    name: 'Tropical Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 11,
    name: 'Greenhouse',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 12,
    name: 'Hobby Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z"
  },
  {
    id: 13,
    name: 'WWOOF',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z"
  },
  {
    id: 14,
    name: 'Organic Farming',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z"
  }
];

const ids = [1, 4, 2];

console.log(categories.filter(x => ids.includes(x.id)))

答案 1 :(得分:0)

您可以使用.reduce来实现此目的,因为您只希望结果中包含idname。如果当前对象的idids数组中,则可以将其添加到acc(累加器)

const categories = [{id:1,name:"Permaculture",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:2,name:"Food Forest",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:3,name:"Community Supported Farming (CSA)",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:4,name:"Urban Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:5,name:"Roof Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:6,name:"Roof Gardening",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:7,name:"Gardening",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:8,name:"Indoor Plantation",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:9,name:"Soil Culture",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:10,name:"Tropical Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:11,name:"Greenhouse",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:12,name:"Hobby Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:13,name:"WWOOF",active:!1,createdAt:"2018-06-18T11:44:56.000Z",updatedAt:"2018-06-18T11:44:56.000Z"},{id:14,name:"Organic Farming",active:!1,createdAt:"2018-06-18T11:44:56.000Z",updatedAt:"2018-06-18T11:44:56.000Z"}],
ids = [1, 4, 2],

res = categories.reduce((acc, {id, name}) => ids.includes(id) ? [...acc, {id, name}] : acc, []);
console.log(res);

但是,如果您关心对象的出现顺序,则可以在上面的输出中使用.map将数字映射到它对应的对象,其中数字等于对象的id对象(使用.find()

const categories = [{id:1,name:"Permaculture",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:2,name:"Food Forest",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:3,name:"Community Supported Farming (CSA)",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:4,name:"Urban Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:5,name:"Roof Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:6,name:"Roof Gardening",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:7,name:"Gardening",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:8,name:"Indoor Plantation",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:9,name:"Soil Culture",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:10,name:"Tropical Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:11,name:"Greenhouse",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:12,name:"Hobby Farming",active:!1,createdAt:"2018-06-18T11:38:41.000Z",updatedAt:"2018-06-18T11:38:41.000Z"},{id:13,name:"WWOOF",active:!1,createdAt:"2018-06-18T11:44:56.000Z",updatedAt:"2018-06-18T11:44:56.000Z"},{id:14,name:"Organic Farming",active:!1,createdAt:"2018-06-18T11:44:56.000Z",updatedAt:"2018-06-18T11:44:56.000Z"}],
ids = [1, 4, 2],

res = ids.map(num => categories.reduce((acc, {id, name}) => ids.includes(id) ? [...acc, {id, name}] : acc, []).find(({id}) => num === id));
console.log(res);

答案 2 :(得分:0)

使用array.filter过滤器将检查对象id是否在id数组内。如果存在,它将插入到数组中。遍历所有元素后,它将返回数组。

const categories = [ 
  { id: 1,
    name: 'Permaculture',
    active: false,
    createdAt: '2018-06-18T11:38:41.000Z',
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 2,
    name: 'Food Forest',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 3,
    name: 'Community Supported Farming (CSA)',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 4,
    name: 'Urban Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 5,
    name: 'Roof Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 6,
    name: 'Roof Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 7,
    name: 'Gardening',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 8,
    name: 'Indoor Plantation',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 9,
    name: 'Soil Culture',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 10,
    name: 'Tropical Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 11,
    name: 'Greenhouse',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 12,
    name: 'Hobby Farming',
    active: false,
    createdAt: "2018-06-18T11:38:41.000Z",
    updatedAt: "2018-06-18T11:38:41.000Z" },
  { id: 13,
    name: 'WWOOF',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z" },
  { id: 14,
    name: 'Organic Farming',
    active: false,
    createdAt: "2018-06-18T11:44:56.000Z",
    updatedAt: "2018-06-18T11:44:56.000Z" } 
];
const ids = [1, 4, 2];
console.log(categories.filter((e)=>ids.indexOf(e.id)!=-1?e:false))