简体   繁体   English

更改属性值的最佳方法。 它在对象数组的对象内

[英]Best way to change a value of a property. It's within an object of an array of objects

I'm trying to switch the value of selected from whatever it currently is (true or false) to the opposite when a user clicks on a checkbox.当用户单击复选框时,我正在尝试将selected的值从当前的任何值(真或假)切换到相反的值。 My toggleCheck function is supposed to return all data with the altered select value.我的toggleCheck函数应该返回具有更改的选择值的所有数据。 Here's the data structure.这是数据结构。 It's an object with arrays that have objects.它是一个包含有对象的数组的对象。

{
    "ModuleName": [{
            "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我最接近的尝试,但格式错误

function toggleCheck(checkedValue, valuesArr) {
    return {
        value: Object.entries(valuesArr).map(([key, values], i) =>
            values.map((x) => {
                return {
                    ...x,
                    selected:
                        x.name === checkedValue.name ? !x.selected : x.selected,
                };
            })
        ),
    };
}

probably cause I used a map it wrapped it in an array可能是因为我使用了一个将其包装在数组中的地图

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

^^^ What I got ^^^ 我得到了什么

{
    "Name": [  {},
               {},
               {}
            ],
    "Name": [...],
    "Name": [...]
}

^^^ What I need ^^^ 我需要什么

function toggleCheck(checkedValue, valuesArr) {

    //Step 0: Change the value in the data
    let checked = Object.entries(valuesArr).map(([key, values], i) =>
        values.map((x) => {
            return {
                ...x,
                selected:
                    x.name === checkedValue.name ? !x.selected : x.selected,
            };
        })
    );

    //Step 1: Group the modules into like arrays
    let grouped = checked .flat().reduce(function (r, a, i) {
        if (!i || r[r.length - 1][0].module !== a.module) {
            return r.concat([[a]]);
        }
        r[r.length - 1].push(a);
        return r;
    }, []);

    //Step 2: Assign the new object properties as the arrays. 
    //The key being the module name.
    let newObj = {};
    grouped.map((x, i) => {
        newObj[x[0].module] = x;
    });

    return {value: newObj};
}

In your question you wrote:在您的问题中,您写道:

{
    "Name": [  {},
               {},
               {}
            ],
    "Name": [...],
    "Name": [...]
}

^^^ What I need ^^^ 我需要什么


this can't be exist in JS object. this 不能存在于 JS 对象中。 Only the last assignment of the name property will be taken into account, all the previous ones are only old values of this property只考虑 name 属性的最后一次赋值,之前所有的只是这个属性的旧值

codding :编码:

const data = 
  { name:'aaa'
  , name:'bbb'
  , name:'ccc' 
  }

is the same as coding和编码一样

const data = {}
data.name = 'aaa'
data.name = 'bbb'
data.name = 'ccc'

proof:证明:

 const data = { name:'aaa' , name:'bbb' , name:'ccc' } console.log( data )
 .as-console-wrapper {max-height: 100% !important;top: 0;} .as-console-row::after {display: none !important;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改属性值的最佳方法。 它位于对象数组的数组中 - Best way to change a value of a property. It's within an array of arrays of objects 更改javascript对象数组中的对象属性的最佳方法是什么? - what's the best way to change an object property in a javascript object array? 给定键时从对象数组中更改 object 值的最佳方法 - best way to change value of an object from an array of objects when given a key 从对象数组中减少属性值的最佳方法 - The best way of reducing a property value from an array of objects 在对象数组中更新 object 属性的最有效方法 - Most efficient way to update an object property within an array of objects 过滤基于 object 的对象数组,其中数组作为其属性值在反应 - filter an array of objects based on an object with an array as it's property value in react 将对象属性链接到值的最佳方法是什么? - What's the best way to keep an object property linked to a value? 如何更改数组中对象的属性? - How do I change an object's property within an array? 在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值 - Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object 无法读取数组属性。 对象作为 React 子对象无效 - Cannot read the array property. Objects are not valid as a React child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM