javascript - 抓取另一个对象中的特定对象

标签 javascript arrays json sorting

我有这个js对象

{
  "2015": {
    "11": {
      "10": {
        "Family & Friends\nGames": {
          "c": 2
        },
        "Collectible\nGames": {
          "c": 1
        },
        "Logic Games\n": {
          "c": 1
        },
        "Kids Games\n": {
          "c": 1
        }
      },      
    },
    "Family & Friends\nGames": {
      "c": 9
    },
    "Collectible\nGames": {
      "c": 2
    },
    "Logic Games\n": {
      "c": 4
    },
    "Kids Games\n": {
      "c": 6
    },
    "Classic Games\n": {
      "c": 3
    },
    "Preschool\nGames": {
      "c": 5
    }
  },
  "meta": {
    "id": [
      "[CLY]2",
      "[CLY]7",
      "[CLY]6",
      "[CLY]1",
      "[CLY]4",
      "[CLY]3"
    ],
    "segments": [
      "id",
      "title"
    ],
    "title": [
      "Family & Friends\nGames",
      "Collectible\nGames",
      "Logic Games\n",
      "Kids Games\n",
      "Classic Games\n",
      "Preschool\nGames"
    ]
  }
}

我试图减去“meta”上方与“title”下的键匹配的相同对象。我使用 ruby​​ 完成了此操作,代码如下

     results = JSON.parse(resp.body)
     data = results["2015"]
     title = results["meta"]["title"]
     alg = data.slice(*title).to_a
     info = alg.sort_by { |k,v| v["c"] }

因此,我尝试将对象转换为数组,然后从那里分割与其他数组名称标题匹配的所有内容,最后根据其“c”值对信息进行排序。

这些似乎都不适合我,所以我尝试使用 [] 和内部键的名称从对象中获取数据,但由于数组“title”下的每个名称都有\n 让我必须添加额外的\来访问它。我希望它是动态的,而不必每次在“标题”下有不同的名称时都必须修改它

我需要这样的东西https://jsfiddle.net/7chntms2/6/

感谢您的帮助

最佳答案

这就是你想要实现的目标吗?

var obj = {
  "2015": {
    "11": {
      "10": {
        "Family & Friends\nGames": {
          "c": 2
        },
        "Collectible\nGames": {
          "c": 1
        },
        "Logic Games\n": {
          "c": 1
        },
        "Kids Games\n": {
          "c": 1
        }
      },
    },
    "Family & Friends\nGames": {
      "c": 9
    },
    "Collectible\nGames": {
      "c": 2
    },
    "Logic Games\n": {
      "c": 4
    },
    "Kids Games\n": {
      "c": 6
    },
    "Classic Games\n": {
      "c": 3
    },
    "Preschool\nGames": {
      "c": 5
    }
  },
  "meta": {
    "id": [
      "[CLY]2",
      "[CLY]7",
      "[CLY]6",
      "[CLY]1",
      "[CLY]4",
      "[CLY]3"
    ],
    "segments": [
      "id",
      "title"
    ],
    "title": [
      "Family & Friends\nGames",
      "Collectible\nGames",
      "Logic Games\n",
      "Kids Games\n",
      "Classic Games\n",
      "Preschool\nGames"
    ]
  }
}

var keys = obj["meta"]["title"];
var newObj = [];

for (var i = 0; i < keys.length; i++) {
  newObj.push({
    name: keys[i],
    content: obj["2015"][keys[i]]
  });
}

newObj.sort(function(a, b) {
  if (a.content.c < b.content.c) return -1;
  if (a.content.c > b.content.c) return 1;
  return 0;
});

console.log(newObj);

关于javascript - 抓取另一个对象中的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295860/

相关文章:

javascript - 将 CSS 和 HTML 汉堡菜单转换为 React 组件

javascript - 如何在html中打印本地存储中数组中存储的值

javascript - 使用 Redux 调度操作获取 TS 错误, `Property does not exist on type`

php - 引用 - 这个错误在 PHP 中意味着什么?

python - 如何在 Numpy 中将索引数组转换为掩码数组?

jquery - JSON:是否可以有一个具有多个值的键?

json - 为什么我的 Mongoose 查询在循环中只返回第一个结果?

ios - 如何在不使用 Markdown 的情况下在 Apple News 格式中使用链接添加(超链接)?

javascript - 如何导出/导入es6 js库

javascript - 使用 JavaScript 检测 NPAPI 支持