首页 > 解决方案 > 使用字符串键映射嵌套的 JavaScript 对象,然后过滤列表

问题描述

我很难通过带有字符串和对象的嵌套对象进行映射,试图获取数组中“数量”值的列表,以便我可以过滤掉。

数据如下所示:

const data = {   
   '123': {
        'name': 'Part 1',
        'size': '20',
        'qty' : '50'
    },
    '5678' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '60'
    },
   '9810' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '120'
    },
 }

// my code I tried:
const getValue = Object.key(data).map(i=> [i].qty)  //undefined
// expect return ['50','60','120']

const items = ['4','120','5']

// Expecting remove '120' from this list, because the getValue should have '120' in the array.
const filterItem = items.filter(i => i !== getValue)

谢谢您的帮助!

标签: javascriptloopsecmascript-6filter

解决方案


像这样试试

Object.keys(data).map(i => data[i].qty)


推荐阅读