Home > Back-end >  Best way to change a value of a property. It's within an array of arrays of objects
Best way to change a value of a property. It's within an array of arrays of objects

Time:06-16

So I'm trying to switch the value of selected from whatever. Value is the opposite. It's for a checkbox. Idk maybe lack sleep but I'm having the hardest time trying to figure this out. Here's the data structure. It's JSON with arrays that have objects.

[
    [
       {
            "module": "string",
            "selected": false,
            "name": "title",
            "points": [{
                    "category": "category1",
                    "description": "desc1",
                    "provided": "provided1"
                },
                {
                    "category": "category1",
                    "description": "desc", 
                    "provided": "something"
                }
            ]
        },
        {
            "module": "string",
            "selected": false,
            "name": "title_one",
            "points": [{
                    "category": "category1",
                    "description": "desc1",
                    "provided": "provided1"
                },
                {
                    "category": "category1",
                    "description": "desc",
                    "provided": "something"
                }
            ]
        },
    ],
    [...],
    [...]
]

My closest attempt but it's the wrong format

//checked value is the information of the checkbox and valuesArr is the entire array
function toggleCheck(checkedValue, valuesArr) {
    return {
        value: valuesArr.map((module, i, ...values) => {
            return [
                ...values,
                (values[i] = module.map((x, j, ...transObj) => ({
                    ...transObj,
                    selected: x.name === checkedValue.name ?
                        !values[i][j].selected :
                        values[i][j].selected,
                }))),
            ];
        }),
    };
}

^^^ That above structure made it so it's

[
    [
        [   {},
            {},
            {}
        ],
        [...],
        [...]
    ]
]

I need

[
    [   {},
        {},
        {}
    ],
    [...],
    [...]
]

CodePudding user response:

I'm not sure if that is what you really need but you can use flat method to put all the json in only one array.

You can use it like this:

const data = yourArray.flat()

And you can iterate de data const in an easier way.

Check this doc to know more about the flat method:

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

CodePudding user response:

Your toggleCheck is pretty hard to read, so I can't tell why it doesn't work the way you expect. A more clean way to do this may be:

function toggleCheck(checkedValue, data) {
  return data.map(
    arr => arr.map(
      obj => {
        if (obj.name == checkedValue.name) {
          obj.selected = !obj.selected
        }
    })
  )
}

Note that this will generate an entirely new object. Not exactly a problem except if the array will contain thousands of objects (maybe not even that!). In your original code, you seem to try to use indexes, I don't know if the intention is to modify the original object, in that case, you can just use forEach instead of map:

function toggleCheck(checkedValue, data) {
  data.forEach(
    arr => arr.forEach(
      (obj, i) => {
        if (obj.name == checkedValue.name) {
          arr[i].selected = !arr[i].selected
        }
    })
  )
}

You don't need to index the first array, indexing the second is enough to get the object's reference and modify the value as expected.

CodePudding user response:

Without knowing all the requirements you have for your code, I'm just focusing on how to remove the unwanted nesting of the arrays. You can simplify your code as well with this solution. Next step would be to decide if you're wanting the values copied / how deep you want to copy the array and if the toggle logic is indeed what you're after.

function toggleCheck(checkedValue, valuesArr) {

  let midArray = [...valuesArr];

  return midArray.map( (module) => {
    return module.map( (values)=>{
      return {
        ...values,
        selected: values.name === checkedValue.name ? !values.selected : values.selected
      }
    })
  }
  )
}
  • Related