Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142239

Javascript - Determine if array should have index set, a new index added, or an existing index set to 0

$
0
0

I'm trying to update an array periodically from an API.

Essentially I have my existing array (what was kept from before the update), and then the new array which I am comparing it to, from the API.

What I want to achieve is, the existing array being either updated with new product information, given a new product, the existing product information changing if it has changed based on the API, and if an existing product no longer exists the stock of it being set to 0.

Taking from this thread here - https://codereview.stackexchange.com/questions/196692/determine-whether-to-push-or-update-object-in-array-based-on-unique-id

   var myData = [
    { product: "Ford", profit: 1000, Stock: "1"},
    { product: "Honda", profit: 1000, Stock: "1"},
];

var APIData = [
    { product: "Ford", profit: 9000, Stock: "1"},
    { product: "Toyota", profit: 1500, Stock: "1"},
];

function compareObjects(obj1, obj2) {
    let obj1Keys = [], obj1Values = [], obj2Keys = [], obj2Values = [];

    for (const [key, value] of Object.entries(obj1)) obj1Keys.push(key) / obj1Values.push(value);
    for (const [key, value] of Object.entries(obj2)) obj2Keys.push(key) / obj2Values.push(value);

    if (obj1Keys.length !== obj2Keys.length) return false;
    for (const key of obj1Keys) {
        if (!obj2Keys.includes(key)) return false;
    }

    for (const value of obj1Values) {
        if (!obj2Values.includes(value)) return false;
    }

    return true;

};

function pushToArray(oldArray, newArray) {
    // first we create a new Array of the existing IDS.
    const newArrayIDS = newArray.map(obj => obj.product);
    // just creating a new array to return when everything is finished
    let newArrayToBeCreated = [];
    // for...loop between the oldArray (your array) 
    for (const oldObject of oldArray) {
        // if the object of the oldArray doesn't have an ID on the API, it will be setted to zero
        if (!newArrayIDS.includes(oldObject.product)) {
            console.log(`Finishing the stock of: ${oldObject.product}`);
            oldObject.Stock = "0";
            newArrayToBeCreated.push(oldObject);
        } else {
            newArray.map(object => {
                // try to found an element by ID 
                let found = oldArray.find(o => o.product === object.product);
                // if the object had found and they are different, just update.
                if (found && compareObjects(found, object) == false) {
                    console.log(`Updating the object: ${found.product}`)
                    found = object;
                    newArrayToBeCreated.push(found);
                } else {
                    newArrayToBeCreated.push(object);
                    console.log(`Adding the object: ${object.product}`);
                };
            });
        }
    }
    return newArrayToBeCreated;
};


/*
Updating the object: Ford
Adding the object: Toyota
Finishing the stock of: Honda
[
  { product: 'Ford', profit: 9000, Stock: '1', id: 1 },
  { product: 'Toyota', profit: 1500, Stock: '1', id: 3 },
  { product: 'Honda', profit: 1000, Stock: '0', id: 2 }
]
*/
console.log(pushToArray(myData, APIData));

So -> I think the above code will work just for adding new information and updating old, when there is information that was there but isn't now, I'm unsure how to proceed.

Appreciate your help :)


Viewing all articles
Browse latest Browse all 142239

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>