gpt4 book ai didi

javascript - 通过查看另一个数组顺序对对象数组进行排序

转载 作者:行者123 更新时间:2023-11-30 08:21:27 25 4
gpt4 key购买 nike

我试图通过查看另一个数组顺序来对对象数组进行排序,我的第一个数组是这样的

["HV001O3XL", "HV001OSML", "HV001OLGE"]

我的对象数组是这样的:

[{productcode: "HV001OSML", price: 6, qty: "2", desc: "HV001 WAISTCOAT ORN", stock: "138"},{productcode: "HV001OLGE", price: 6, qty: "1", desc: "HV001 WAISTCOAT ORN", stock: "271"},{productcode: "HV001O3XL", price: 6, qty: "1", desc: "HV001 WAISTCOAT ORN", stock: "1112"}]

我想重新排列对象数组,以便 object.productcode 匹配第一个数组,首先是 HV001O3XL 而不是 HV001OSML 等。这在 Angular 或 javascript 中可能吗?

最佳答案

const objs = [
{
productcode: "HV001OSML",
price: 6,
qty: "2",
desc: "HV001 WAISTCOAT ORN",
stock: "138"
},
{
productcode: "HV001OLGE",
price: 6,
qty: "1",
desc: "HV001 WAISTCOAT ORN",
stock: "271"
},
{
productcode: "HV001O3XL",
price: 6,
qty: "1",
desc: "HV001 WAISTCOAT ORN",
stock: "1112"
}
];
const result = [
"HV001O3XL",
"HV001OSML",
"HV001OLGE"
].map((key) => objs.find(item => item.productcode === key));
console.log(result);

关于javascript - 通过查看另一个数组顺序对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52950341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com