在javascript字典中查找项,给定一个项数组

rur96b6h  于 2023-03-16  发布在  Java
关注(0)|答案(4)|浏览(113)

我有一个对象
values = [19, 20, 23]
另一种具有以下结构

options = [{
  id = 19.0,
  label = USA
}, {
  label = Europe,
  id = 20.0
}, {
  label = Asia ex - China ex - India,
  id = 21.0
}, {
  label = China,
  id = 22.0
}, {
  label = India,
  id = 23.0
}, {
  id = 24.0,
  label = Israel
}, {
  id = 25.0,
  label = Africa
}, {
  label = Latam,
  id = 26.0
}, {
  id = 27.0,
  label = Oceania
}]

我需要帮助来编写一个生成以下格式对象的函数
['USA', 'Europe', 'India']
我现在正在使用
value.forEach( a => options.find(b === b.id = a))
但并没有产生我想要的结果。

e3bfsja2

e3bfsja21#

forEach方法不创建新数组,您必须使用map
注意你的options.find方法调用应该是一个回调函数。

let result = values.map((a) => {
  return options.find(b => a === b.id);
})
console.log(result)
<script>
  const values = [19, 20, 23],
    options = [{
      id: 19.0,
      "label": "USA"
    }, {
      "label": "Europe",
      id: 20.0
    }, {
      "label": "Asia ex - China ex - India",
      id: 21.0
    }, {
      "label": "China",
      id: 22.0
    }, {
      "label": "India",
      id: 23.0
    }, {
      id: 24.0,
      "label": "Israel"
    }, {
      id: 25.0,
      "label": "Africa"
    }, {
      "label": "Latam",
      id: 26.0
    }, {
      id: 27.0,
      "label": "Oceania"
    }]
</script>
0pizxfdo

0pizxfdo2#

过滤器更优雅。你也有浮动的ID

let result = options
  .filter(({id}) => values.includes(parseInt(id)))
  .map(({label}) => label);

console.log(result)
<script>
  const values = [19, 20, 23],
    options = [{
      id: 19.0,
      "label": "USA"
    }, {
      "label": "Europe",
      id: 20.0
    }, {
      "label": "Asia ex - China ex - India",
      id: 21.0
    }, {
      "label": "China",
      id: 22.0
    }, {
      "label": "India",
      id: 23.0
    }, {
      id: 24.0,
      "label": "Israel"
    }, {
      id: 25.0,
      "label": "Africa"
    }, {
      "label": "Latam",
      id: 26.0
    }, {
      id: 27.0,
      "label": "Oceania"
    }]
</script>
hgc7kmma

hgc7kmma3#

如果id是浮点值,则必须减去当前值并检查浮点错误。

**注意:**我选择了0.00000001(或1e-8)的任意精度

const
  options = [
    { id: 19.0, label: 'USA' },
    { label: 'Europe', id: 20.0 },
    { label: 'Asia ex-China ex-India', id: 21.0 },
    { label: 'China', id: 22.0 },
    { label: 'India', id: 23.0 },
    { id: 24.0, label: 'Israel' },
    { id: 25.0, label: 'Africa' },
    { label: 'Latam', id: 26.0 },
    { id: 27.0, label :'Oceania' }
  ],
  values = [19, 20, 23];

const countries = options
  .filter(({ id }) => values.find(value => Math.abs(value - id) < 0.00000001))
  .map(({ label }) => label);

console.log(JSON.stringify(countries)); // [ "USA","Europe","India" ]
qq24tv8q

qq24tv8q4#

reduce是你要找的。

const options = [
  {id: 19.0, label: 'USA'},
  {id: 20.0, label: 'Europe'},
  {id: 21.0, label: 'Asia ex-China ex-India'}
];

const toLookFor = [19, 20, 23];

console.log(
  options.reduce(
    (accumulator, currentValue) => {
      if(toLookFor.includes(currentValue.id)){
        accumulator.push(currentValue.label)
      };
      
      return accumulator
    },
    []
  )
);

相关问题