如何对字符串数据进行分类 javascript

How to classify String data javascript

本文关键字:分类 javascript 数据 字符串      更新时间:2023-09-26

我想对这个数据主体、标题等进行分类。

{
    "offset": "0",
    "results": [
        {
            "body": "A novelist, essayist, historian and critic, Geoff Dyer writes about whatever happens to interest him. ''I am the opposite of an expert,'' he said in an e-mail message, ''a case study in how not to go about building a career as a writer.'' It's been quite a non-career. ''Yoga for People Who Can't Be Bothered to Do It'' won the W. H. Smith Best",
            "byline": "By THE EDITORS",
            "date": "20071028",
            "title": "Up Front",
            "url": "http://query.nytimes.com/gst/fullpage.html?res=9E0CE4D61130F93BA15753C1A9619C8B63"
        },
        {
            "body": "PRINCETON, N.J. Many of us have known this scholar: The hair is well-streaked with gray, the chin has begun to sag, but still our tortured friend slaves away at a masterwork intended to change the course of civilization that everyone else just hopes will finally get a career under way. We even have a name for this sometimes pitied species -- the",
            "byline": "By JOSEPH BERGER",
            "date": "20071003",
            "title": "ON EDUCATION; Exploring Ways to Shorten the Ascent to a Ph.D.",
            "url": "http://www.nytimes.com/2007/10/03/education/03education.html"
        }
    ]
}

结果

body == {"A novelist, essayist, historian and critic, Geoff Dyer writes about whatever happens to interest him. ''I am the opposite of an expert,'' he said in an e-mail message, ''a case study in how not to go about building a career as a writer.'' It's been quite a non-career. ''Yoga for People Who Can't Be Bothered to Do It'' won the W. H. Smith Best",
            "byline": "By THE EDITORS", PRINCETON, N.J. Many of us have known this scholar: The hair is well-streaked with gray, the chin has begun to sag, but still our tortured friend slaves away at a masterwork intended to change the course of civilization that everyone else just hopes will finally get a career under way. We even have a name for this sometimes pitied species -- the"}
byline == ["By THE EDITORS", "By JOSEPH BERGER",}
data == ["20071028", "20071003"]

您必须遍历数据并执行所需的操作(假设它存储在名为 data 的变量中)

for (var i = 0; i < data.results.length; i++) {
    console.log(data.results[i].body); //Or whatever you need
    ..
    ..
    ..
}
var data = JSON.parse(json);
var output = {};
for (var i = 0, l = data.results.length; i < l; i++) {
  var el = data.results[i];
  for (var p in el) {
    if (el.hasOwnProperty(p)) {
      if (!output[p]) { output[p] = []; }
      output[p].push(el[p]);
    }
  }
}
console.log(output);