1

Let's say I have this object:

{
   b954WYBCC4YbsMM36trawb00xZ32: { activity1: "pending", activity2: "pending" },
​   pby0CAqQ1hTlagIqBTQf6l2Ti9L2: { activity1: "pending", activity2: "pending" }
}

where the first keys are IDs from a database. I want to be able to change the values of activity1 and activity2 based on an array:

// 'data' is the object mentioned above.
const usersHaveSpecialContentAssigned = [true, false];

Object.values(data).forEach((item, index) => {
    const arrayActivityProgress = Object.values(item as string[]);
    if (usersHaveSpecialContentAssigned[index] === false) {
        arrayActivityProgress.forEach((progress) => {
            progress = 'unassigned';
            console.log(progress);
        });
    }
});

As you can see, my goal is to change the values of activity1 and activity2 if the corresponding value in usersHaveSpecialContentAssigned is false.

The end result based on the example above should look like this:

{
   b954WYBCC4YbsMM36trawb00xZ32: { activity1: "pending", activity2: "pending" },
​   pby0CAqQ1hTlagIqBTQf6l2Ti9L2: { activity1: "unassigned", activity2: "unassigned" }
}

How can I achieve this? My current approach doesn't update the values.

3
  • primitive values aren't referenced values, so changing one variable holding a primitive value has no effect on another variable assigned the same value. If you want to mutate the objects in data you need to do so explicitly ie. item.activity1 = 'unassigned' Commented Dec 14, 2022 at 23:24
  • Firstly you are better off passing in true or false into your function rather than an index. As in any language, false = 0 and true = 1, whereas your index is the opposite, but it is just easier to pass true or false. Commented Dec 14, 2022 at 23:39
  • Hint: if I do const o = [1]; let [a] = o; a = 3; console.log(o[0]); it logs 1 not 3. Assigning to a function parameter in the forEach callback doesn't change the array. Commented Dec 15, 2022 at 0:24

2 Answers 2

2

As you already know what the ids are going to be, I would write it this way.

var initialData = {
   b954WYBCC4YbsMM36trawb00xZ32: { activity1: "pending", activity2: "pending" },
​   pby0CAqQ1hTlagIqBTQf6l2Ti9L2: { activity1: "pending", activity2: "pending" }
};
var permissionsPerUserDataIn = {"b954WYBCC4YbsMM36trawb00xZ32": true, "pby0CAqQ1hTlagIqBTQf6l2Ti9L2": false, "id3": false};

and write your function like this:

function fixData(permissionsPerUser) {

    for (var [id, values] of Object.entries(initialData)) {
       if (permissionsPerUser.hasOwnProperty(id) && !permissionsPerUser[id]) {  
           values.activity1 = 'unassigned';
           values.activity2 = 'unassigned';
           console.log(`${id}: `, values);
       }
    }
}

fixData(permissionsPerUserDataIn);

Doing it this way will give you more granular control over what permissions will get updated and you will not have to worry if a value in the array gets out of sync for whatever reason.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is the closest to the solution I came up with, and in fact, I used your answer as a general baseline. Thanks!
0

You can write

Object.values(data).forEach((item, index) => {
    if (!usersHaveSpecialContentAssigned[index])
        item.activity1 = item.activity2 = "unassigned";

You of course need to watch for the index not to be out of range.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.