for-of,取出关联数组中的指定数据

a、b是2个关联数组

const a=[
  {
    "id": "6d5c54f",
    "label": "scene",
    "index": 0,
  },
  {
    "id": "6cd50b2e",
    "label": "select",
    "index": 1,
  },
  {
    "id": "60aa1bb8",
    "label": "filter",
    "index": 2,
  }
]

const b=[
  "6cd50b2e",
  "60aa1bb8"
]

b数组的元素正好==a数组某几个元素的id

取出b数组对应a数组的label。

const inputs=[]

for(let x of a){
  for(let id of b){
    if(x.id===id){
      inputs.push(x.label)
    }
  }
}
console.log(inputs)//[select, filter]
原文地址:https://www.cnblogs.com/wuhairui/p/14000145.html