Javascript 在阵列中添加数据阵列的最佳方法是什么?

Javascript 在阵列中添加数据阵列的最佳方法是什么?,javascript,arrays,redux,Javascript,Arrays,Redux,我有一个状态对象,其中有一个类别数组。在这个对象数组中,对象的一个键(“列表”)被分配一个数组。 我需要做的是 按名称筛选类别数组中的特定对象 将新对象添加到筛选对象的“列表”属性(数组)(保留旧对象) 示例 const initialState = { category: [ { name: "new", color: "#5236C1", list: [

我有一个状态对象,其中有一个类别数组。在这个对象数组中,对象的一个键(“列表”)被分配一个数组。 我需要做的是

  • 按名称筛选类别数组中的特定对象
  • 将新对象添加到筛选对象的“列表”属性(数组)(保留旧对象)
  • 示例

    const initialState = {
        category: [
            {
                name: "new",
                color: "#5236C1",
                list: [
                    {
                        title: "name title",
                        about: "about",
                        imgLink: 'https://www.google.com/s2/favicons?domain=https://mail.yandex.ru/',
                        url: 'https://www.yandex.ru'
                    }, 
                     // there should be a new object
                ]
            },
    
    试图这样做


    应该有一个新对象

    我修改了addBM函数,并添加了一些解释代码的注释

    const addBM = (state, payload) => {
        console.log(payload)
        const {url, title, about, cat} = payload
    
        // use map method instead of filter
        const modifiedCategory = state.category.map(categoryItem => {
            // only modify the category item if it's name matches the cat argument
            // otherwise return the original category item
            
            if (categoryItem.name == cat) {
                // create a copy of the category item object
                let modifiedCategoryItem = {
                    ...categoryItem,
                    // modify the list array of this object by
                    // creating a copy of the array and
                    // adding a new object with (url, title and about arguments) in this array
                    list: [...categoryItem.list, {url, title, about}]
                };
                return modifiedCategoryItem;
            } else {
                return categoryItem;
            }
        });
    
        return {
            ...state,
            category: modifiedCategory
        }
    };
    

    您想如何处理过滤后的数组(
    selectcatar
    )?从哪里来的?(如果您能展示一个输入、有效负载对象和预期结果的示例,这将非常有用)有点相关:您在这里编写了一个非常旧的redux样式,因此您可能遵循了一个过时的教程。在现代redux中(*仅当您使用官方redux工具包和createSlice/createReducer时),您可以简单地
    。将
    推入该数组,而无需自己考虑不可变的修改-工具包将为您完成这一任务。看看这个。大多数其他来源已经严重过时。(另外,你剩下的代码也会缩减到1/4左右)@Imngz,哦,我的天,现在我要try@Imngz,非常感谢。真管用!
    
    const addBM = (state, payload) => {
        console.log(payload)
        const {url, title, about, cat} = payload
    
        // use map method instead of filter
        const modifiedCategory = state.category.map(categoryItem => {
            // only modify the category item if it's name matches the cat argument
            // otherwise return the original category item
            
            if (categoryItem.name == cat) {
                // create a copy of the category item object
                let modifiedCategoryItem = {
                    ...categoryItem,
                    // modify the list array of this object by
                    // creating a copy of the array and
                    // adding a new object with (url, title and about arguments) in this array
                    list: [...categoryItem.list, {url, title, about}]
                };
                return modifiedCategoryItem;
            } else {
                return categoryItem;
            }
        });
    
        return {
            ...state,
            category: modifiedCategory
        }
    };