转换Json数据

时间:2018-10-27 13:03:48

标签: javascript arrays json

Json

    [
{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},
{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},
{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}
]

我想获取diff变量中的每一行,例如:

{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0}

data: [0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0]

使用javascript

我正在尝试

$(document).ready(function () {

        $.ajax({
            type: "Post",
            url: "FeedBackGraph.aspx/getdata",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (Response) {
                debugger;
                var d = Response.d.toString();
                var final_string = d;

                var res = final_string.split(",").map(Number);
                console.log(final_string);

5 个答案:

答案 0 :(得分:3)

map覆盖对象,并返回减去第一个元素的值。您可以使用它们的索引访问这些新数组。

const arr = [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}];

const out = arr.map(a => Object.values(a).slice(1));
console.log(JSON.stringify(out[0]));

这是一个可以满足vlaz担心的问题,因为密钥的顺序可能不正确:

const arr = [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}];

// get the keys from the first object
const keys = Object.keys(arr[0])

  // remove stars
  .filter(el => el !== 'stars')

  // sort by key q1 to q11
  .sort((a, b) => +a.match(/\d+/) > +b.match(/\d+/));

// map over the data again and build new arrays based on the
// ordered keys
const out = arr.map(({stars, ...el}) => {
  return keys.reduce((acc, c) => acc.concat(el[c]), [])
}, []);

console.log(JSON.stringify(out[0]));

答案 1 :(得分:3)

您可以使用map()遍历数组。使用destructuring assignment删除您不想包含的属性(stars),然后使用Object.values将对象转换为数组。

var arr = [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}]

var result = arr.map(({stars,...r}) => Object.values(r))

console.log(result);


如果需要,数组的顺序将基于键。您可以创建一个order变量。在map()的第一个循环上分配顺序。使用Object.keys获取所有密钥。使用sort()对键进行排序。

使用另一个map()循环遍历order变量并构造一个新数组以返回。

var arr = [{"stars":1,"q2":0,"q1":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}];

var order = null;
var result = arr.map(({stars,...r}) => {
  if (order === null) order = Object.keys(r).sort((a, b) => a.localeCompare(b, undefined, {numeric: true,sensitivity: 'base'}));
  return order.map(o => r[o]);
});

console.log(result);

答案 2 :(得分:1)

您可以使用函数reduce来构建数组,并使用函数Object.values来提取每个对象的值。

let arr = [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},{"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},{"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},{"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}],
    result = arr.reduce((a, c) => a.concat({data: Object.values(c).slice(1)}), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:1)

let data = (row)=>{
  let {q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11}=row;
  return [q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11];
};

data(json[0]); 
  

输出 [0,0,0,0,0,2,0,0,0,0,0]

  • 我假设数据只有q1到q11
  • json是响应对象

答案 4 :(得分:0)

there is no guarantee of the property order in an object起,您需要采取预防措施以确保获得正确的结果。

var data = [
  {"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":2,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
  {"stars":2,"q1":2,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
  {"stars":3,"q1":1,"q2":0,"q3":0,"q4":1,"q5":1,"q6":1,"q7":0,"q8":1,"q9":1,"q10":0,"q11":1},
  {"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":1,"q8":0,"q9":0,"q10":1,"q11":0},
  {"stars":5,"q1":0,"q2":3,"q3":3,"q4":2,"q5":2,"q6":0,"q7":2,"q8":2,"q9":2,"q10":2,"q11":2}
]

var convertedObject = transformObject(data[0]);
console.log("transforming single object", convertedObject);

var convertedArray = data.map(transformObject);
console.log("transforming the entire array", convertedArray);


function convertKeyToNumeric(key) {
  var numericString = key.slice(1); //remove the first character 
  return Number(numericString);
}

function transformObject(obj) {
  return Object.keys(obj) //get keys
    .filter(key => key !== "stars") //remove "stars"
    .sort((a, b) => convertKeyToNumeric(a) - convertKeyToNumeric(b)) //sort in ascending order
    .map(key => obj[key]) //get the values
}

  • Object.keys会提取密钥,因此您可以使用它们。
  • Array#filter排除了不需要的键。
  • Array#sort确保属性的正确顺序,因此不会以错误的顺序处理它们,例如q8-> q6-> q10-> q7-> q9-> q2-> q1-> q5-> q3-> q11-> q4
  • 然后
  • Array#map为您提供值。

为清楚起见,提取了convertKeyToNumeric函数。可以根据获得的键的类型以多种方式重写它。这仅适用于以单个非数字字符开头的字符。