简体   繁体   English

我在react中有数组的平坦状态,我想使其成为嵌套数组并将其推到另一个对象ins状态

[英]i have flat state of arrays in react and i want to make it nested array and push it to another object ins state

I have two states in a react component, 我在React组件中有两种状态,

    class ProductsideBar extends Component {

    constructor(props){
        super(props)     

         this.state = {
            Catgeory: [
                {id:'1' , name:'parent_1' , parentId:'0'},
                {id:'2' , name:'child_1' , parentId:'1'},
                {id:'3' , name:'child_2' , parentId:'1'},

                {id:'4' , name:'parent_2' , parentId:'0'},
                {id:'5' , name:'child_1' , parentId:'4'},
                {id:'6' , name:'child_2' , parentId:'4'},
            ],
              subCatgeory:[]
              nestedCategory:[];
        }

....

I want a javascript function that changes state. 我想要一个可更改状态的JavaScript函数。 Category to something like this bellow nested array and add to state.nestedCategory .. 类别类似于此波纹管嵌套数组,然后添加到state.nestedCategory ..

nestedCategory: [
    {
        id:'1' ,
        name:'parent_1' ,
        parentId:'0',
        subCategory: [{
            id:'2' ,
            name:'child_1',
            parentId:'1'
        }, {
            id:'3' ,
            name:'child_2' ,
            parentId:'1'
        }]
    }, {
        id:'4' ,
        name:'parent_2' ,
        parentId:'0',
        subCategory: [{
            id:'5' ,
            name:'child_1',
            parentId:'4'
        }, {
            id:'6' ,
            name:'child_2' ,
            parentId:'4'
        }]
    },
]

Then i can do anything with nestedCategory. 然后,我可以使用nestedCategory做任何事情。

You can do something like this. 你可以做这样的事情。 I haven't included the logic for finding the subcategory, that is easy but this one liner code should help. 我没有包括找到子类别的逻辑,这很简单,但是这一行代码应该会有所帮助。

Edit : Added a proper solution. 编辑:添加了适当的解决方案。

this.setState(
    {
        nestedCategory : this.state.category.map((value, index) => {
            const nobj = {...value};
            nobj.subCategory = this.state.subcategory.filter((value2) =>  value2.parentId === value.id); 
            return nobj;
         })
    }
) 

暂无
暂无

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

相关问题 我有一个具有父ID的平面数组,我想使其嵌套,并且像一个带有javascript的树父子数组 - i have a flat arrays that have parent ids and i want to make it nested and like a tree parent child array with javascript 我想展平嵌套的对象数组并将它们转换为一个对象数组 - I want to flat a nested arrays of objects and convert them into one object array 反应我想在数组中添加一个 state - react I want to add a state to the array 我试图将一个对象添加到我存储在我的状态对象中的嵌套数组中,但似乎有问题 - I am trying to add an object to a nested array I have stored in my state object, but seem to have an issue with it 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 我想将 arrays 推入数组 - i want to push arrays into array 当内容嵌套在数组中时,React 不会更新(我已确保 state 更新) - React does not update when content is nested in array (i have made sure that the state update) 更新 React 中的嵌套数组 Object 数组 State - Update nested Array Object Array in React State 我有一个平面对象数组,我想将它与对象的子数组合并 - I have a flat array of object and i want to merge it with child array of object 在React中更新状态:嵌套在Object中的数组元素 - Update state in React: array element nested in Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM