0

Below are 2 objects what I want is Object.assign(initalPlan, data), but I want to delete the fields also that are not provided in data.

const initialPlan = {
        default_plan: false,
        external_plan_id: fakeString,
        public_available: true,
        name: fakeString,
        price: fakeNumber,
        pricing_period: fakeNumber,
        pricing_period_unit: fakeString,
        space_limit: fakeNumber,
        staff_max_limit: fakeNumber,
        staff_min_limit: fakeNumber,
        trial_period: fakeNumber,
        trial_period_unit: fakeString,
    };

and other object:

const data = {
            external_plan_id: plan.external_plan_id,
            space_limit: plan.space_limit,
            staff_min_limit: plan.staff_min_limit,
            staff_max_limit: plan.staff_max_limit,
            file_storage_limit: plan.file_storage_limit,
            file_storage_limit_unit: plan.file_storage_limit_unit,
            default_plan: plan.default_plan,
        };

Any ideas?

Haris Khan
  • 199
  • 1
  • 1
  • 10
  • 6
    You can't, `object.assign` does not delete properties, all it does is a shallow copy of the properties in the sources to the target. You most likely want to iterate and manually copy properties instead. – adeneo Nov 02 '16 at 13:19
  • You could override the Object.assign function to do what you want. But you definitely shouldn't. What you should do is write a function that uses hasOwnProperty ? copyValue : null. – Bob Brinks Nov 02 '16 at 13:21
  • 1
    Do you absolutely *want* to update the properties `initialData`? If you assigned a new object you could use a [pick](http://stackoverflow.com/questions/25553910/one-liner-to-take-some-properties-from-object-in-es-6) function. – CodingIntrigue Nov 02 '16 at 13:33
  • 1
    The question is ambiguous. Do you want to copy properties from `data` to `initialPlan` or only delete properties. If it's only the former, then why is `initialPlan` constant? If it's the latter, then why do you talk about `Object.assign`? – a better oliver Nov 02 '16 at 13:50
  • 2
    How is what you want to do different than `Object.assign({}, data);`? –  Nov 02 '16 at 13:57
  • I'm with @torazaburo on this. The result of your desired computation will be exactly equal to `data`. – Thank you Nov 02 '16 at 17:16
  • @torazaburo you are right, I was trying to make jasmine test as real as I could, but ended up copying the data object for tests. The function is was trying to test, took the plan argument and made PUT with data object. – Haris Khan Nov 04 '16 at 09:30

2 Answers2

0

After you have your data and initialPlan objects, you can do something like this to remove the properties on initialPlan that aren't present on data. Then you could use Object.assign(initialPlan, data).

const dataKeys = Object.keys(data);
const initialPlanKeys = Object.keys(initialPlan);

initialPlanKeys.forEach(k => {
  if (!dataKeys.includes(k))
    delete initialPlan[k];
});
Gavin
  • 3,468
  • 1
  • 14
  • 25
  • @torazaburo `Object.assign(initialPlan, data)` right before my code example. Didn't include it in the code block because the OP stated that's what he was trying to use. – Gavin Nov 02 '16 at 17:05
-1

You should use Object.assign and remove all the keys that are not part of data

const initialPlan = {
  key1: 1,
  key2: 2,
  key3: 3
}
const data = {
  key1: 10,
  key2: 0
}

Object.assign(initialPlan, data);
Object.keys(initialPlan).forEach(function(key){
   if(!data.hasOwnProperty(key)) delete initialPlan[key];
});
console.log(initialPlan)
taguenizy
  • 1,922
  • 1
  • 6
  • 23
  • Why not just remove **all** keys from `initialPlan` to start with? –  Nov 02 '16 at 16:34
  • @torazaburo that was my initial thought but he could be using that `initialPlan` and that `data` could be just to update it – taguenizy Nov 02 '16 at 16:37