skip to Main Content

I’m trying to make a simple app to update prices in WooCommerce through the REST API.

I have arrayOne which has SKU + updated prices from my ERP

[
 {sku=PD-1000-B, price=9800.00},
 {sku=PD-1007-A, price=9678.16}
]

arrayTwo which has all products from woocommerce, SKU, id (which I need to get) and current price

[
 {sku=PD-1000-B, id=1622.0, price=8145.9},
 {sku=PD-1007-A, id=1624.0, price=9678.16}
]

I would like to compare arrayTwo with arrayOne, if price in arrayOne is bigger then with all coincidences combine and create new array. Something like this

[
 {sku=PD-1000-B, id=1622.0, price=9800.00}
]

Since the only bigger price is for PD-1000-B.

All I got so far is to create a new Set but that doesn’t compare anything, the idea would be to reduce only to changes the amount to update.

EDIT: As of right now I’m merely replacing the price in arrayTwo with the one in arrayOne using this:

let set = new Set();
for (let i = 0; i < data.length; i++)
    set.add(data[i]['sku'])
for (let i = 0; i < container.length; i++)
    if (set.has(container[i]['sku']))
        container[i]['price'] = data[i]['price'];

container is an empty variable I’m using to collect the data I get from WooCommerce as an array and data is a the array which contains the updated prices + sku.

Thank you.

2

Answers


  1. You can achieve this in two steps:

    1. Find greatest ones of each in arrOne
    2. Compare with corresponding one in arrTwo

    You can use this nice function for that:

    const arrOne = [
      {
        sku: "PD-1000-B",
        price: 9678.16
      },
      {
        sku: "PD-1000-B",
        price: 9800.00
      },
      {
        sku: "PD-1007-C",
        price: 9714.16
      },
      {
        sku: "PD-1007-C",
        price: 9270.00
      }
    ]
    
    const arrTwo = [
     {sku: "PD-1000-B", id:1622.0, price:8145.9},
     {sku: "PD-1007-A", id:1624.0, price:9678.16},
     {sku: "PD-1007-C", id:1627.0, price:9478.16},
    ]
    
    const findAllHighest = (arr) => {
      // 1) get highest one of each in arrOne
      const arrOneGreatest = arr.reduce((prev, cur) => {
        if(!prev[cur.sku]){
          prev[cur.sku] = cur
          return prev
        }
    
        if(cur.price > prev[cur.sku].price){
          prev[cur.sku] = cur
        }
        return prev
      }, {})
      
      // match to arrayTwo
      const result = arrTwo.map(el => {
        const arrOneObj = Object.values(arrOneGreatest).find(n => n.sku === el.sku)
    
        if(!arrOneObj)
          return el
    
        if(arrOneObj.price > el.price)
          el.price = arrOneObj.price
        return el
      }, [])
      
      return result
    }
    
    console.log(findAllHighest(arrOne))
     .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. I’d try it this way:

    var arrayOne = [
        {sku: 'PD-1000-B', price: 9800.00},
        {sku: 'PD-1007-A', price: 9678.16}
    ];
    
    var arrayTwo = [
        {sku: 'PD-1000-B', id: 1622.0, price: 8145.9},
        {sku: 'PD-1007-A', id: 1624.0, price: 9678.16}
    ];
    
    // convert the array into the object {sku1: {}, sku2: {}, ...}
    var arrayOne_obj = {}
    for (let obj of arrayOne)
        arrayOne_obj[obj.sku] = {'price': obj.price};
    
    // convert the array into the object {sku1: {}, sku2: {}, ...}
    var arrayTwo_obj = {}
    for (let obj of arrayTwo)
        arrayTwo_obj[obj.sku] = {'price': obj.price, 'id': obj.id};
    
    // loop through the 'sku'-subobjects of the 'arrayOne_obj' object
    // compare its prices with prices of the same 'sku'-subobject of 'arrayTwo_obj',
    // and make the output array
    var output = [];
    for (let sku in arrayOne_obj) {
        let subObjOne = arrayOne_obj[sku];
        let subObjTwo = arrayTwo_obj[sku];
        if (subObjOne.price > subObjTwo.price) {
            output.push({'sku':sku, 'id': subObjTwo.id, 'price':subObjOne.price});
        }  
    }
    
    console.log(output); // output [ {sku: 'PD-1000-B', id:1622.0, price:9800.00} ]
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search