Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用纯JS或Lodash基于数组对集合密钥进行排序_Arrays_Sorting_Collections_Lodash - Fatal编程技术网

Arrays 使用纯JS或Lodash基于数组对集合密钥进行排序

Arrays 使用纯JS或Lodash基于数组对集合密钥进行排序,arrays,sorting,collections,lodash,Arrays,Sorting,Collections,Lodash,我想根据数组顺序对产品集合键进行排序。我期待的是像期待的那样的输出。有没有人在纯JS或Lodash库中有一个简单的解决方案 您可以从 谢谢 const=require('lodash')) var乘积=[ {cgst:“18”,项目:“UPS V-1450”,价格:“2800”,sgst:“18”,库存:2}, {cgst:“9”,项目:“UPS V Guard Prime 1450”,价格:“6000”,sgst:“9”,库存:30} ] 风险值顺序=['item'、'price'、'cgs

我想根据数组
顺序
产品
集合键进行排序。我期待的是像
期待的
那样的输出。有没有人在纯JS或Lodash库中有一个简单的解决方案

您可以从

谢谢

const=require('lodash'))
var乘积=[
{cgst:“18”,项目:“UPS V-1450”,价格:“2800”,sgst:“18”,库存:2},
{cgst:“9”,项目:“UPS V Guard Prime 1450”,价格:“6000”,sgst:“9”,库存:30}
]
风险值顺序=['item'、'price'、'cgst'、'sgst'、'stock']
//期望输出
var预期=[
{项目:“UPS V-1450”,价格:“2800”,cgst:“18”,sgst:“18”,库存:2},
{项目:“UPS V Guard Prime 1450”,价格:“6000”,cgst:“9”,sgst:“9”,库存:30}

]
您的问题实际上是排序CSV字段,而不是对象属性。 该方法接受具有
字段
数据
的对象。您可以将订单传递给它:

var产品=[
{cgst:“18”,项目:“UPS V-1450”,价格:“2800”,sgst:“18”,库存:2},
{cgst:“9”,项目:“UPS V Guard Prime 1450”,价格:“6000”,sgst:“9”,库存:30}
]
风险值顺序=['item'、'price'、'cgst'、'sgst'、'stock']
var csv=Papa.unpasse({
字段:顺序,
数据:产品
});
console.log(csv)

为什么需要对对象的属性进行排序?我想使用Papa Parse将其转换为CSV文件。因此,我想保持数据与数组
顺序相同。这两种方法非常有效!非常感谢你。这将是您使用
map()
.pick()
所做的最短解决方案。我将使用Para.unpasse()解决方案,因为它不太容易出错,而且可能更高效。