Reputation: 1640
I have a JSON OBJECT
{"data":{"source1":"source1val","source2":"source2val"}}
which i want to convert into
data : source1val, source2val.
Upvotes: 2
Views: 1958
Reputation: 27466
var input = {
"data": {
"source1": "source1val",
"source2": "source2val"
}
};
var output = [];
var i;
for (i = 0; i < input.data.length; i++) {
output.push(input.data[i]);
}
Upvotes: 0
Reputation: 36609
Use Object.keys
with Array#map
The
Object.keys()
method returns an array of a given object's own enumerable properties.
The
map()
method creates a new array with the results of calling a provided function on every element in this array.
var input = {
"data": {
"source1": "source1val",
"source2": "source2val"
}
};
var output = Object.keys(input.data).map(function(k) {
return input.data[k];
}).join(',');
console.log(output); //manipulated object
console.log(input); //Original object
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Upvotes: 3