0

I have json data like this.

[{"data":"85"},{"data":"83"},{"data":"75"},{"data":"87"},{"data":"86"},{"data":"0"},{"data":"84"}].

I wanted to remove the "data": and curly brackets.

I wanted the output to be like this.

[85,83,75,87,86,0,84]

Someone please help me on converting it to like that.

1
  • 2
    You need to know how to access objects in JS, and how to use a for loop. Enjoy your googling session! Commented Apr 1, 2014 at 12:58

5 Answers 5

2

You tagged your question with jQuery, so heres an answer using it:

var input = [{ "data": "85" }, { "data": "83" }, { "data": "75" }, { "data": "87" }, { "data": "86" }, { "data": "0" }, { "data": "84" }];
var output = $.map(input, function (e) { return e.data; });
Sign up to request clarification or add additional context in comments.

Comments

1
var newArray = [];
jsonData.forEach(function(i) {
    newArray.push(i.data);
});

Where jsonData is the name of the variable storing your JSON data.

3 Comments

forEach is awefull why do people even use it ?
Why do you think that? Because of browser support?
many reasons. browsers supports, inability to break it(well not easy way)... etc
1

Loop through the array and extract data value like this

var obj= [{"data":"85"},{"data":"83"},{"data":"75"},{"data":"87"},{"data":"86"},{"data":"0"},{"data":"84"}];
var arr = [];
for ( var i = 0; i < obj.length;i++){
      arr.push(obj[i].data);
}

Comments

0

Please check the following code.

var myMessage = [{"data":"85"},{"data":"83"},{"data":"75"},{"data":"87"},{"data":"86"},{"data":"0"},{"data":"84"}];

var obj2 = eval(myMessage);
var myArray = new Array();
for(var i in obj2){
     myArray[i] = obj2[i].data;
}
console.log(myArray);

Cheers Subh

Comments

0
var msg = '[{"data":"85"},{"data":"83"},{"data":"75"},{"data":"87"},{"data":"86"},{"data":"0"},{"data":"84"}]';
var msgObject = JSON.parse(msg);  
var output = new Array();

for (var i = 0; i < msgObject.length; i++) {
  output.push(msgObject[i].data);
}
alert(JSON.stringify(outputObject));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.