如何通过另一个数组数字键过滤数组键

How to filter array key by another array number key

我有 2 个数组,第一个数组结构是:

items: [
    {
      name: "a",
      items: [
         { name: "jack" },
         { name: "jose" },
      ]
    },
    {
      name: "b",
      items: [
         { name: "lara" },
         { name: "jo" },
      ]
    },
    {
      name: "c",
      items: [
         { name: "andy" },
         { name: "hary" },
      ]
    }
]

和第二个数组:

number: [
  0: [0, 1],
  1: [1],
  2: [0]
]

如何按“数量”过滤“项目”以及如何获得这样的输出? (最佳方案)

{["jack", "jole"],["jo"],["andy"]}

几张地图就可以做到:

你想要的输出不是有效的 JS 所以我做了一个嵌套数组

const arr1 = [{ name: "a", items: [{ name: "jack" }, { name: "jose" }, ] }, { name: "b", items: [{ name: "lara" }, { name: "jo" }, ] }, { name: "c", items: [{ name: "andy" }, { name: "hary" }, ] } ], numbers = [ [0, 1], [1], [0] ]; 

const res = numbers
  .map((arr, i) => arr
    .map(key => arr1[i].items[key].name)
  )
console.log(res)

如果你的数字变量必须是一个对象。

let items = [
  {
    name: "a",
    items: [{ name: "jack" }, { name: "jose" }]
  },
  {
    name: "b",
    items: [{ name: "lara" }, { name: "jo" }]
  },
  {
    name: "c",
    items: [{ name: "andy" }, { name: "hary" }]
  }
];

let number = {
  0: [0, 1],
  1: [1],
  2: [0]
};

let result = []
for (const [key, value] of Object.entries(number)){
  let names = []
  value.forEach(value => {
    names.push(items[key].items[value].name)
  })
  result.push(names)
}

console.log(result)