3

I've got this forEach that first checks if a property is there and then deletes it. Then if the property is not defined in existingParams it appends it again with the new value. It looks like this:

parameters = parameters || {};

let existingParams = this.getUrlParameters();

 _.forEach(parameters, function (value, key) {
            if ((value !== null || typeof value !== 'undefined')
                && existingParams.hasOwnProperty(key)
            ) {
                //First console.log

                console.log("Deleted!: " + existingParams[key])

                delete existingParams[key];
                return;
            } else if (value === null || typeof value === 'undefined') {
                return;
            }

            //Second console.log

            console.log(existingParams);

            if ( ! existingParams.hasOwnProperty(key)) {
                Object.defineProperty(existingParams, key, {
                    value: value,
                    enumerable: true,
                    configurable: true
                });

                return;
            }

            existingParams[key] = value;
        });

The problem is that it never delete's a param delete existingParams[key]; (first if statement). In the first console.log I see that the correct param is being deleted. But in the second console.log the param is still there!?!?!?

How is this possible?

existingParams is for example: {minPrice: "1021", maxPrice: "2751", page: 1}

parameters is for example: {minPrice: 1301, maxPrice: 3081}

Jenssen
  • 1,801
  • 4
  • 38
  • 72
  • 1
    Can you please provide some example values for `parameters` and `existingParams`? – Bergi Oct 23 '17 at 11:32
  • Yes sure one moment, – Jenssen Oct 23 '17 at 11:32
  • That happens to me sometimes as well, an object turns out to be immutable for some reason. I need to [deep clone the object](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object), then it works – Jeremy Thille Oct 23 '17 at 11:33
  • @JeremyThille hmm how would that look like in my example? – Jenssen Oct 23 '17 at 11:35
  • @Bergi updated my answer. – Jenssen Oct 23 '17 at 11:42
  • 3
    Please add a [minimal, complete, and **verifiable** example](https://stackoverflow.com/help/mcve/), because: https://jsfiddle.net/jnko6hvk/ – Andreas Oct 23 '17 at 11:44
  • 1
    I don't quite get your question. From what I can see, you want to check if a key from `parameters` is in `existingParams`. If so, delete it from *existingParams*. I tried this in JSBin and your function works. With your object examples, then second console.log is never even triggered. the `return` statement from the if/else check at the start of each iteration are stopping the second part of the code being executed Can you clarify exactly what you want to do in a broader sense? – Jayce444 Oct 23 '17 at 12:00
  • @Jayce444 thanks for your time. I want to update the `existingParams` with the `parameters`. If `existingParams` is this: `{minPrice: "1000", maxPrice: "2000", page: 2}` and parameters is this: `{minPrice: "2000", maxPrice: "3000", page: 2}` existing parameters should update to: `{minPrice: "2000", maxPrice: "3000", page: 2}` if you've any questions please let me know. – Jenssen Oct 23 '17 at 12:06
  • 2
    @Jenssen I don't see how you would need to delete any property for that - just overwrite them with the new values. No `delete`, no `Object.defineProperty`, just a loop and an assignment - or even trivially `Object.assign(existingParams, parameters);` – Bergi Oct 23 '17 at 12:12
  • @Jenssen Please also add the exact output. Also, make sure that the input params are correct and return statement works fine – Rishikesh Chandra Oct 23 '17 at 12:18
  • @Bergi thanks! that fixed it :) – Jenssen Oct 23 '17 at 12:35

0 Answers0