[英]How to get all ordered values from immutable.js orderedmap without toJS?
I have such an orderedmap like this:我有一个这样的有序地图:
{
"200": { id: 200, name: "John" },
"120": { id: 120, name: "Mike" },
"350": { id: 350, name: "James" }
}
How to get all ordered values without toJS method?如何在没有toJS方法的情况下获取所有有序值?
I've tried:我试过了:
map.valueSeq().toArray(), Array.from(map.values())
but it returns an intermixed array
.但它返回一个intermixed array
。
You could do toList()
to get all the values
(ei not the keys
) of the map in a List
immutable structure you can further manipulate later, or do List(yourMap)
if you want the key
and value
as a tuple array of sorts.您可以执行toList()
以在List
不可变结构中获取地图的所有values
(即不是keys
),您可以稍后进一步操作,或者如果您希望key
和value
作为排序的元组数组,则执行List(yourMap)
. Per the docs :根据文档:
This is similar to List(collection), but provided to allow for chained expressions.这与 List(collection) 类似,但提供以允许链式表达式。 However, when called on Map or other keyed collections, collection.toList() discards the keys and creates a list of only the values, whereas List(collection) creates a list of entry tuples.但是,当在 Map 或其他键控集合上调用时,collection.toList() 会丢弃键并创建一个仅包含值的列表,而 List(collection) 创建一个条目元组列表。
const { Map, List } = require('immutable') var myMap = Map({ a: 'Apple', b: 'Banana' }) List(myMap) // List [ [ "a", "Apple" ], [ "b", "Banana" ] ] myMap.toList() // List [ "Apple", "Banana" ]
Javascript will sort your source object
keys if they are numeric or parseable as numeric, so you can do the following workaround:如果源object
键是数字或可解析为数字,Javascript 将对它们进行排序,因此您可以执行以下解决方法:
const PeopleMap = new OrderedMap([
["200", { id: 200, name: "John" }],
["120", { id: 120, name: "Mike" }],
["350", { id: 350, name: "James" }]
]);
Here's a working example: https://jsfiddle.net/8kbcyfsn/这是一个工作示例: https : //jsfiddle.net/8kbcyfsn/
By declaring it as a key value pair array, the ordered map registers the right order.通过将其声明为键值对数组,有序映射注册了正确的顺序。
Whereas if you declare it as an object,而如果你将它声明为一个对象,
const PeopleMap = new OrderedMap({
"200": { id: 200, name: "John" },
"120": { id: 120, name: "Mike" },
"350", { id: 350, name: "James"}
});
It will attempt to order it by the Number(key)
value.它将尝试按Number(key)
值对其进行排序。
我不确定我是否正确理解了您的问题,但如果我理解了,您可以尝试:
Object.keys(obj).map(key => obj[key]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.