简体   繁体   中英

Array inside an Object with Dynamic Key in Redux

Given, I have an array

var myKey = "myObjectKey";
var anArray = ["apple", "balloon", "dog", "cat"];

anArray.map(function(thing) {
  store.dispatch(Object.assign({type: 'ADD_ITEM', payload: thing, key: myKey}));
});

How do I get the following output?

store.getState();
# Object: {myObjectKey: ["apple", "balloon", "dog", "cat"]}

This should work. Just pass the object to the dispatch. It will be handelled in the reducer

var myKey = "myObjectKey";
var anArray = ["apple", "balloon", "dog", "cat"];

  store.dispatch({type: 'ADD_ITEM', payload: { [myKey]: anArray} })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM