最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Easy way to extract json object properties into an array? - Stack Overflow

programmeradmin1浏览0评论

Given the following JSON object, is there an easy way to extract just the values of the results object properties?

var j={"success":true,
       "msg":["Clutch successfully updated."],
       "results":{"count_id":2,
                  "count_type":"Clutch",
                  "count_date":"2000-01-01",
                  "fish_count":250,
                  "count_notes":"test"}
      };

var arr= doSomething(j.results);
//arr=[2, "Clutch","2000-01-01",250,"test"]

Given the following JSON object, is there an easy way to extract just the values of the results object properties?

var j={"success":true,
       "msg":["Clutch successfully updated."],
       "results":{"count_id":2,
                  "count_type":"Clutch",
                  "count_date":"2000-01-01",
                  "fish_count":250,
                  "count_notes":"test"}
      };

var arr= doSomething(j.results);
//arr=[2, "Clutch","2000-01-01",250,"test"]
Share Improve this question asked Apr 29, 2011 at 18:08 dnagirldnagirl 20.4k13 gold badges85 silver badges126 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Your function would be something like

var doSomething = function (obj) {
    var arr = [];
    for (var x in obj) if (obj.hasOwnProperty(x)) {
        arr.push(obj[x]);
    }
    return arr;
}
function resultstoArray (resultsData) {
  var myArray = new Array();
  for (var key in resultsData) {
    myArray.push(resultsData[key]);
  }
  return myArray;
}

var arr = resultsToArray(j.results);
发布评论

评论列表(0)

  1. 暂无评论