您能帮忙了解如何组合两个具有不同结构的不同数组中的数据吗?
我通过 API 获取数据,第一个请求:
[
{
"category_id": "12345",
"category_name": "itemgroup 1",
"category_is_hidden": "0",
"product_id": "9999999",
"product_name": "item 1",
"product_sku": "0002344",
"product_brand": null,
"product_vendor_code": "art 123",
"is_productset": "0",
"productset_items": null,
"is_service": "0",
"is_serial": "0",
"variations": {
"id": "1122334455",
"name": "red",
"measurement_unit": "unit_piece",
"quantity_minimal_piece": "1.000",
"sku": null,
"vendor_code": "",
"retail_price": "2550.00",
"wholesale_price": "2550.00",
"discount_type": "percent",
"discount": "0.00",
"barcodes": ["2000471000002", "2000481000009"]
}
}
]
第二个要求:
[
{
"9999999": {
"company_id": 63,
"branch_id": 69,
"variation_id": 954629,
"supplier_id": 0,
"quantity": 1
}
}
]
我需要通过product_id匹配数据并获得以下格式的输出: 产品名称、数量
此刻我只想到:
async function getstockandproductinfo() {
try {
const stockinforesponse = await axios.get(`${BASE_URL}/stock?branch_id=0`, {
headers: {'API-KEY': API_KEY}
});
const productinforesponse = await axios.get (`${BASE_URL}/products`, {
headers: {'API-KEY': API_KEY}
});
const productdata = productinforesponse.data;
const stocksdata = stockinforesponse.data;
const meld = [].concat(productdata, stocksdata);
meld.forEach(item => {
if (typeof item === 'object' && item !== null) {
for (const product_id in item) {
if (item[product_id] && item[product_id].hasOwnProperty('quantity')) {
const quantity = item[product_id].quantity;
console.log(`Product ID: ${product_id}, Quantity: ${quantity}`);
}
}
}
});
} catch (error) {
console.error('Error', error);
}
}
getstockandproductinfo();
当我尝试获取产品名称时,我得到了未定义的信息
我会很高兴获得任何帮助,因为我是一个十足的菜鸟)
以下是一种方法:
// create a hashMap out of obj1 so that Key is product_id and value is the whole object.. this will help us to look up fast
const obj1ReduceToAMap = obj1.reduce((res, val) => ({...res, [val.product_id]: val}), {});
// iterate over obj2..
obj2.forEach(val => {
//get all the objects that can be there for each element, and get the corresponding name from obj1ReduceToAMap
Object.keys(val).forEach(key => console.log("product_id: ", key, "quantity: ", val[key].quantity, "name: ", obj1ReduceToAMap[key].product_name));
});
您可以循环第二个数组,找到每个元素的键,通过filter()在第一个数组中搜索它,如果找到,则通过循环复制适当键处的值:
let firstArray = [
{
"category_id": "12345",
"category_name": "itemgroup 1",
"category_is_hidden": "0",
"product_id": "9999999",
"product_name": "item 1",
"product_sku": "0002344",
"product_brand": null,
"product_vendor_code": "art 123",
"is_productset": "0",
"productset_items": null,
"is_service": "0",
"is_serial": "0",
"variations": {
"id": "1122334455",
"name": "red",
"measurement_unit": "unit_piece",
"quantity_minimal_piece": "1.000",
"sku": null,
"vendor_code": "",
"retail_price": "2550.00",
"wholesale_price": "2550.00",
"discount_type": "percent",
"discount": "0.00",
"barcodes": ["2000471000002", "2000481000009"]
}
}
];
let secondArray = [
{
"9999999": {
"company_id": 63,
"branch_id": 69,
"variation_id": 954629,
"supplier_id": 0,
"quantity": 1
}
}
];
for (let product of secondArray) {
let product_id = Object.keys(product)[0];
let found = firstArray.filter(item => item.product_id === product_id);
let index = firstArray.indexOf(found[0]);
if (index >= 0) {
for (let key in product[product_id]) {
firstArray[index][key] = product[product_id][key];
}
}
}
console.log(firstArray);