简体   繁体   English

使用“unshift”方法将项目添加到 Map Object 的开头

[英]Add item to beginning of an Map Object with "unshift" method

  const { items, ids } = action.payload;
  const { fetchedItems } = state;
  const diff = _.difference(state.ids, action.payload.ids);
  // @APPEND
  for (let i = 0; i < items.length; i++) {
    fetchedItems.set(items[i].id, items[i]);
  }
  // @DELETE
  for (let i = 0; i < diff.length; i++) {
    fetchedItems.delete(diff[i]);
  }
  const myArray = Array.from(fetchedItems.values());
  return {
    ...state,
    items: Array.from(fetchedItems.values()), // not for use
    fetchedItems,
    ids: action.payload.ids,
    syncStatus: action.payload.tocMeta.syncStatus
  };

Quesstion is: my API based on sockets and every time i am receive new object, i am use Map object for ease update and delete and everything working nice, i want to set new items which coming from action.payload object on the first index of the Map "like unshift", can you provide some trick for that? Quesstion is: my API based on sockets and every time i am receive new object, i am use Map object for ease update and delete and everything working nice, i want to set new items which coming from action.payload object on the first index of Map“像 unshift”,你能提供一些技巧吗? Because new Map().Set() put the new items in the bottom of the Map object, and i don't want to do Map.clear() every time.因为 new Map().Set() 将新项目放在 Map object 的底部,我不想每次都做 Map.clear()。

I don't think there's a nice way to do it. 我不认为有什么好方法。 The order of a Map is determined by insertion order, so if you want an a specific value to be first, then your only option is to see to it that it gets inserted first. Map的顺序由插入顺序决定,因此,如果要先指定特定值,则唯一的选择是先插入该值。

I'm also not sure if using Map is actually a good idea in this case. 我也不确定在这种情况下使用Map是否真的是一个好主意。 In Redux, you're not supposed to mutate the state like you do in your for-loops (modifying the Map). 在Redux中,您不应该像在for循环中那样修改状态(修改Map)。 That makes Maps pretty hard to work with in Redux, as you would need to constantly make copies of your Map before you could safely use methods like .set or .delete . 这使得Maps在Redux中很难使用,因为在安全使用.set.delete类的方法之前,您需要不断制作Map的副本。

The creator of Redux has said (about Set and Map) Redux的创建者说过(关于Set和Map)

I don't think it's a good idea to use them in Redux because they are optimized for mutability. 我认为在Redux中使用它们不是一个好主意,因为它们针对可变性进行了优化。 If you want to have something efficient with similar semantics, consider using Immutable.js. 如果您想使用相似的语义获得高效的东西,请考虑使用Immutable.js。

So I would probably switch back to using an array which have convenient non-mutating methods like .map and .filter (and works with the ... syntax), or maybe an object (with id as key, and you can use your ids to keep track of the order). 因此,我可能会改回使用具有便捷的非变异方法(如.map.filter (并适用于...语法)的数组),或者使用对象(将id作为键,并且可以使用ids跟踪订单)。

you have a 'fetchedItems' variable which is a Map (probably a state) and you want to put an item in the first place... when you perform a setState you can replace the 'fetchedItem' with a prevState你有一个'fetchedItems'变量,它是一个Map(可能是一个状态),你想把一个项目放在第一位......当你执行setState时,你可以用prevState替换'fetchedItem'

` `

    const fetchedItemsArray = Array.from(fetchedItems.entries())

or in a more modern way:或更现代的方式:

    const fetchedItemsArray = [...fetchedItems.entries()]

then you can say:那么你可以说:

    fetchedItems = new Map([

        [newValue.id, newValue], => firstItem

        ...fetchedItemsArray => spread all of the other items

     ])

or in a setState action like this:或在这样的 setState 操作中:

     setFetchedItems( prev => new Map([

                        [newValue.id, newValue],
                        ...Array.from(prev.entries())
                    ]));

` I hope this is helpful, the Map constructor accepts a list of arrays in which the first item is the key and the second is the value ` 我希望这会有所帮助,Map 构造函数接受 arrays 列表,其中第一项是键,第二项是值

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

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