0

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}
]

i want to get each row in diff variable like:

{"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]

using javascript

i am trying this

$(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);
Ele
  • 33,468
  • 7
  • 37
  • 75
aparna rai
  • 823
  • 10
  • 24

5 Answers5

3

map over the objects and return their values minus the first element. You can access these new arrays using their indexes.

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]));

And here's a version that will satisfy vlaz's concern with the issue of the keys possibly being in the wrong order:

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]));
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Object property order is not guaranteed, hence the order of the values is also not guaranteed. – VLAZ Oct 27 '18 at 13:23
  • 1
    ["The ordering of the properties is the same as that given by looping over the property values of the object manually."](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values) - so I'm going to assume the OP is ok with that. – Andy Oct 27 '18 at 13:25
  • You could still have `q6` showing up before `q1`. And OP may not even be aware of that. Too many people aren't. – VLAZ Oct 27 '18 at 13:26
  • Updated my answer @vlaz – Andy Oct 27 '18 at 13:45
3

You can use map() to loop thru the array. Use destructuring assignment to remove the property you dont want to include (stars) and use Object.values to convert the object into an array.

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);

If you want the order of the array will be based on the key. You can create an order variable. Assign the order on the first loop of the map(). Use Object.keys to get all the keys. Use sort() to sort the keys.

Use another map() to loop thru the order variable and construct a new array to return.

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);
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • Object property order is not guaranteed, hence the order of the values is also not guaranteed. – VLAZ Oct 27 '18 at 13:24
  • 1
    I think everyone gets the message. – Andy Oct 27 '18 at 13:26
  • Do they? Three different high rep users came up with the same potentially wrong solution when this is one of the most common problems of newbies here on SO - people assuming that the property order of objects is "set". – VLAZ Oct 27 '18 at 13:28
1

You can use the function reduce to build the array along with the function Object.values to extract the values for each object.

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; }
Ele
  • 33,468
  • 7
  • 37
  • 75
  • Object property order is not guaranteed, hence the order of the values is also not guaranteed. – VLAZ Oct 27 '18 at 13:23
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]); 

Output [0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0]

  • I am assuming that data has q1 to q11 only
  • json is the response object
varun sharma
  • 1,017
  • 15
  • 19
0

Since there is no guarantee of the property order in an object, you need to take precautions to make sure you get the correct result.

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 extracts the keys, so you can work with them.
  • Array#filter excludes the keys you don't want.
  • Array#sort ensures the correct order of your properties, so they won't get processed in the wrong order, for example q8 --> q6 --> q10 --> q7 --> q9 --> q2 --> q1 --> q5 --> q3 --> q11 --> q4
  • Array#map then gives you the values.

The convertKeyToNumeric function is extracted for clarity. It can be re-written in a variety of ways, depending on the type of keys you get. This is strictly for ones that start with a single non-numeric character.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Thankx for answer but i am facing ajax response error in this method, like : var data = [Response from ajax] format is into "" braces like :"[{values}]" and when use slice for split " from stating and ending of string this is remove [] from string not "" – aparna rai Oct 29 '18 at 06:50
  • 1
    @aparnarai this is because you are getting an *object* and turning it into a *string* when you call `Response.d.toString()`. You can just get `Response.d` in order to get it and process it as an object. If that's already a string, then `.toString()` is not needed at all but you should do `JSON.parse(Response.d)`. – VLAZ Oct 30 '18 at 13:42