小编Baw*_*bak的帖子

编写一个函数来对对象数组进行排序(通过使用另一个对象来指定排序路径和顺序)

我将编写一个函数,通过使用另一个对象作为其参数来确定根据哪个键执行哪种排序,以某种方式对任何JSON进行排序(仅知道它是对象的数组,例如产品列表)。

// The json that I get might looks like something like this.
// I just write one item of the array, but all of them are the same.
// But the blueprint of the items in each json are different.
const dataArray = [
  {
    id: 100,
    name: 'product_1',
    price: 99.95,
    color: ['#fff', '#f0f', '#00f'],
    category: ['cat_1', 'cat_2'],
    booleanKey: true,
    foo: {
      foo1: 12,
      foo2: 'string value',
      foo3: {
        foo3_1: 'string value',
        foo3_2: 732.342
      },
      foo4: [
        {
          foo4_1_1: …
Run Code Online (Sandbox Code Playgroud)

javascript sorting json object ecmascript-6

5
推荐指数
1
解决办法
91
查看次数

标签 统计

ecmascript-6 ×1

javascript ×1

json ×1

object ×1

sorting ×1