如何连接到数组中的 'indexed' 个对象
how to concat to an 'indexed' objects in array
我有 reducer 改变结构 [{}] 或 [{},{}]
[0:{}] 或 [0:{},1:{}]
进行此更改后,我无法连接到这个原始数组。这种情况有什么解决办法吗?
我必须补充一点,它真的很难改变 reducer,因为它没有生成改变的数组结构(这是我的观点)
我添加reducer代码:
import * as actionTypes from '../actions';
import {schema, normalize, arrayOf} from 'normalizr';
import _ from 'lodash';
const iniState = {
recipes : []
}
function createRecipe(state = iniState, action) {
switch (action.type) {
case actionTypes.ADD_RECIPE:
let newRecipe = {
id: Math.floor(Math.random()*10000),
re: action.payload.recipeInReducer,
c: action.payload.compsInReducer
}
return {
...state,
recipes: state.recipes.concat(newRecipe)
}
case actionTypes.EDIT_BOOLEAN:
return {...state,
recipes: {..._.map(state.recipes, recipe=>{
if(recipe.id === action.payload.idFromRecipe){
return Object.assign({}, recipe, {
c: {..._.map(recipe.c, comp=>{
if(comp.id === action.payload.idFromComp){
return {...comp,
toRecipe: !comp.toRecipe};
}else{return comp}
})
}
})
} else{
return Object.assign({}, recipe, {
c: {..._.map(recipe.c, comp=>comp)}})
}
})
}
}
}
return state;
}
export default createRecipe;
您可以使用 Object.assign
转换对象,它将键作为数组的索引。
var object = { 0: { foo: 0 }, 1: { bar: 42 } },
array = Object.assign([], object);
console.log(array);
进行此更改后,我无法连接到这个原始数组。这种情况有什么解决办法吗? 我必须补充一点,它真的很难改变 reducer,因为它没有生成改变的数组结构(这是我的观点)
我添加reducer代码:
import * as actionTypes from '../actions';
import {schema, normalize, arrayOf} from 'normalizr';
import _ from 'lodash';
const iniState = {
recipes : []
}
function createRecipe(state = iniState, action) {
switch (action.type) {
case actionTypes.ADD_RECIPE:
let newRecipe = {
id: Math.floor(Math.random()*10000),
re: action.payload.recipeInReducer,
c: action.payload.compsInReducer
}
return {
...state,
recipes: state.recipes.concat(newRecipe)
}
case actionTypes.EDIT_BOOLEAN:
return {...state,
recipes: {..._.map(state.recipes, recipe=>{
if(recipe.id === action.payload.idFromRecipe){
return Object.assign({}, recipe, {
c: {..._.map(recipe.c, comp=>{
if(comp.id === action.payload.idFromComp){
return {...comp,
toRecipe: !comp.toRecipe};
}else{return comp}
})
}
})
} else{
return Object.assign({}, recipe, {
c: {..._.map(recipe.c, comp=>comp)}})
}
})
}
}
}
return state;
}
export default createRecipe;
您可以使用 Object.assign
转换对象,它将键作为数组的索引。
var object = { 0: { foo: 0 }, 1: { bar: 42 } },
array = Object.assign([], object);
console.log(array);