如何从数组列表中查找对象

How to find an object from array list

我有一个所有可用库存项目的列表,具有唯一的库存 ID 和详细信息属性。从UI,我收集客户选择,根据客户选择中的唯一值代码,我想从库存项目中找到选择的项目。也就是说,

availableStock 中找到与 value 代码匹配的 stockItem 的最佳方法是什么customerSelection 数组列表中的属性?请看下面的例子:

const customerSelection = {
    "tv": [
        {
            "value": "tv_40",
            "label": "Flat Screen"
        }
    ],
    "keyboard": [
        {
            "value": "keyboard_40",
            "label": "Wireless Keyboard"
        }
    ],
    "mouse": [
        {
            "value": "mouse_40",
            "label": "Wireless Mouse"
        }
    ]
};
const availableStock = [
    {
        "stockId": "elec_c1_f1",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_41"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_41"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_41"
            }
        ]
    },
    {
        "stockId": "elec_c1_f2",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_40"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_40"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_40"
            }
        ]
    },
    {
        "stockId": "elec_c1_f3",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_42"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_42"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_42"
            }
        ]
    }
]

stockId 的预期结果(或只是 stockId)

const stockItem = {
    "stockId": "elec_c1_f2",
    "attributes": [
        {
            "label": "Flat Screen",
            "value": "tv_40"
        },
        {
            "label": "Wireless Keyboard",
            "value": "keyboard_40"
        },
        {
            "label": "Wireless Mouse",
            "value": "mouse_40"
        }
    ]
}

如有任何建议 with/without lodash,我们将不胜感激。

使用嵌套 for..in 循环来导航两个对象。

const customerSelection = {
    "tv": [
        {
            "value": "tv_41",
            "label": "Flat Screen"
        }
    ],
    "keyboard": [
        {
            "value": "keyboard_40",
            "label": "Wireless Keyboard"
        }
    ],
    "mouse": [
        {
            "value": "mouse_40",
            "label": "Wireless Mouse"
        }
    ]
};
const availableStock = [
    {
        "stockId": "elec_c1_f1",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_41"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_41"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_41"
            }
        ]
    },
    {
        "stockId": "elec_c1_f2",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_40"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_40"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_40"
            }
        ]
    },
    {
        "stockId": "elec_c1_f3",
        "attributes": [
            {
                "label": "Flat Screen",
                "value": "tv_42"
            },
            {
                "label": "Wireless Keyboard",
                "value": "keyboard_42"
            },
            {
                "label": "Wireless Mouse",
                "value": "mouse_42"
            }
        ]
    }
];

for (o in customerSelection)
for (p in availableStock)
for (q in availableStock[p].attributes) 
if (customerSelection[o][0].value==availableStock[p].attributes[q].value) console.log(availableStock[p].stockId);

首先从客户选择中获取所有密钥:

var values = Object.keys(customerSelection).map(element =>customerSelection[element][0].value)

然后使用此代码获取 matchedAvailableValues:

var matchedAvailableValues = availableStock.filter(element => values.includes(element.attributes[0].value) ? {stockId:element.stockId,attributes:element.attributes} : false);

因此您的最终代码如下所示:

var values = Object.keys(customerSelection).map(element => customerSelection[element][0].value)
var matchedValues = availableStock.filter(element => values.includes(element.attributes[0].value) ? {stockId:element.stockId,attributes:element.attributes}:false);

我建议使用 Array.values() and Array.map().

创建一个 requiredValues 数组

这看起来像:

[ "tv_40", "keyboard_40", "mouse_40" ]

然后我们将使用 Array.filter() 查找所有库存商品,其属性中存在每个必需值。

我们将使用 Array.every(), along with Array.find() 来确保 每个 必需的值都存在于项目属性中。

这将是 return 库存项目列表。

然后我们可以映射以获取匹配股票 ID 的列表:

const customerSelection = { "tv": [ { "value": "tv_40", "label": "Flat Screen" } ], "keyboard": [ { "value": "keyboard_40", "label": "Wireless Keyboard" } ], "mouse": [ { "value": "mouse_40", "label": "Wireless Mouse" } ] };

const availableStock = [ { "stockId": "elec_c1_f1", "attributes": [ { "label": "Flat Screen", "value": "tv_41" }, { "label": "Wireless Keyboard", "value": "keyboard_41" }, { "label": "Wireless Mouse", "value": "mouse_41" } ] }, { "stockId": "elec_c1_f2", "attributes": [ { "label": "Flat Screen", "value": "tv_40" }, { "label": "Wireless Keyboard", "value": "keyboard_40" }, { "label": "Wireless Mouse", "value": "mouse_40" } ] }, { "stockId": "elec_c1_f3", "attributes": [ { "label": "Flat Screen", "value": "tv_42" }, { "label": "Wireless Keyboard", "value": "keyboard_42" }, { "label": "Wireless Mouse", "value": "mouse_42" } ] } ]
    
// These values must be present in the attributes...
const requiredValues = Object.values(customerSelection).map(val => val[0].value);

// Use Array.filter() to find stock items that match _all_ required attributes...
const foundItems = availableStock.filter(item => {
    return requiredValues.every(value => item.attributes.find(attr => attr.value === value));
});

console.log('Found stockIds:', foundItems.map(({ stockId }) => stockId));
console.log('Found items:', foundItems);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您也可以在不先构建 requiredValues 数组的情况下执行此操作,只需在 customerSelection 数组上调用 .every() 即可:

const customerSelection = { "tv": [ { "value": "tv_40", "label": "Flat Screen" } ], "keyboard": [ { "value": "keyboard_40", "label": "Wireless Keyboard" } ], "mouse": [ { "value": "mouse_40", "label": "Wireless Mouse" } ] };
const availableStock = [ { "stockId": "elec_c1_f1", "attributes": [ { "label": "Flat Screen", "value": "tv_41" }, { "label": "Wireless Keyboard", "value": "keyboard_41" }, { "label": "Wireless Mouse", "value": "mouse_41" } ] }, { "stockId": "elec_c1_f2", "attributes": [ { "label": "Flat Screen", "value": "tv_40" }, { "label": "Wireless Keyboard", "value": "keyboard_40" }, { "label": "Wireless Mouse", "value": "mouse_40" } ] }, { "stockId": "elec_c1_f3", "attributes": [ { "label": "Flat Screen", "value": "tv_42" }, { "label": "Wireless Keyboard", "value": "keyboard_42" }, { "label": "Wireless Mouse", "value": "mouse_42" } ] } ]

const foundItems = availableStock.filter(item => {
    return Object.values(customerSelection).every(([{ value }]) => { 
        return item.attributes.find(attr => attr.value === value);
    });
});

console.log('Found stockIds:', foundItems.map(({ stockId }) => stockId));
console.log('Found items:', foundItems);
.as-console-wrapper { max-height: 100% !important; top: 0; }