I have the following JSON Array
var myObj = [{
1: ['one'],
2: ['two'],
3: ['three']
}];
I want to convert this to an array like below
['one', 'two', 'three']
I have the following JSON Array
var myObj = [{
1: ['one'],
2: ['two'],
3: ['three']
}];
I want to convert this to an array like below
['one', 'two', 'three']
Keeping in mind that object keys have no guaranteed order, I'd get all the entries, sort them on the index, and them map them to the first index of their values.
var myObj = [{
1: ['one'],
2: ['two'],
3: ['three']
}];
var myArr = Object.entries(myObj[0])
.sort(([k1, _], [k2, __]) => k1 - k2)
.map(([_, [v]]) => v);
console.log(myArr);
One way of doing it in jquery-
var myObj = [{
1: ['one'],
2: ['two'],
3: ['three']
}];
var res = [];
$.each(myObj[0], function(index, value) {
res.push(value[0]);
});
console.log(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Object.keys(myObj[0]).map(item => myObj[0][item][0])
While object's keys with stringed integers are sorted by their value, you could take just the values and concat an array.
var array = [{ 3: ['three'], 1: ['one'], 2: ['two'] }],
result = array.reduce((r, o) => r.concat(...Object.values(o).map(([a]) => a)), []);
console.log(result);
var myObj = {
1: ['one'],
2: ['two'],
3: ['three']
};
var array1 = [];
for (var i = 1; i < 4; i++) {
var j = myObj[i];
array1.push(j);
}
console.log(array1);
If Your Object parameters are numbered u can do that, if not try to arrange it in to some kind of order so u can loop it
hope it works