数组:
nameList = ["食品类", "服装类", "工具类", "鞋帽类", "水果类", "海鲜类"]
数组:
valueList = [{product_category: "食品类", allprice: 310}, {date: "水果类", allprice: 115}]
答案:
let nameList = ["食品类", "服装类", "工具类", "鞋帽类", "水果类", "海鲜类"];
let valueList = [{ product_category: "食品类", allprice: 310 }, { product_category: "水果类", allprice: 115 }];
let arrayTOobject = Object.fromEntries(
valueList.map(({ product_category, allprice }) => [product_category, allprice])
);
let newValueList = nameList.map(item => arrayTOobject[item] || 0);
console.log(newValueList); // [310, 0, 0, 0, 115, 0]