This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I need to convert JSON object to javascript array. This my object
var object =
[
{
"Key": "2019",
"value": 3
},
{
"Key": "2020",
"value": 3
},
{
"Key": "2021",
"value": 3
}
]
i need to weel be my array like this ['2019','2020','2021']
i try to to this
var data_t = [];
for (var i in object ) {
data_t.push(object );
}
console.log(data_t[0]);
But not working !!
You can use .map() for this:
var object = [
{
"Key": "2019",
"value": 3
},
{
"Key": "2020",
"value": 3
},
{
"Key": "2021",
"value": 3
}
];
console.log(object.map(a => a.Key));
var data_t = [];
for (var i in object ) {
data_t.push(i.Key);
}
Related
This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Serializing an object to JSON
(4 answers)
Closed 4 months ago.
I have this array:
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
And i need to convert it to this stringified format:
"{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
I have made several attempts without luck.
My last attempt was this one:
array = array.map((p) => {
return Object.values(p).join(':').replace(/\[/g, '{').replace(/]/g, '}').toString();
}),
Any solutions?
Simply make a empty object, iterate over your array to populate that object, then JSON.stringify(obj) to get your result.
Like this:-
var obj = {};
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
for(let i of array) {
obj[i.name] = i.value;
}
const str = JSON.stringify(obj);
console.log(str);
/*
output : "{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
*/
I would like to convert the below array of objects into another form (varname is ignored as its not required, key and value is used to generate the output form). Any leads would be appreciated
Input array:
[
{
"key": "string_U6",
"value": "grandwagoneer",
"varname": "pagenameplate"
},
{
"key": "string_U13",
"value": "2021",
"varname": "year"
}
]
Output
[
{
"string_U6": "grandwagoneer"
},
{
"string_U13": "2021"
}
]
You could try using map as below:
var input = [ { "key": "string_U6", "value": "grandwagoneer", "varname": "pagenameplate" }, { "key": "string_U13", "value": "2021", "varname": "year" } ];
var output = input.map(function(entry){
let obj = {};
obj[entry.key] = entry.value;
return obj;
});
console.log(output);
As the question asked how to convert array object in snowflake, I wanted to share Snowflake way to do it:
-- SQL to create a sample table with data
create table sample_table (v variant )
as select parse_json(' [ { "key": "string_U6", "value": "grandwagoneer", "varname": "pagenameplate" },
{ "key": "string_U13", "value": "2021", "varname": "year" } ]');
-- query to parse the variant and create the array:
select ARRAY_AGG( OBJECT_CONSTRUCT(i.value:key::varchar, i.value:value::varchar) )
from sample_table,
lateral flatten ( sample_table.v ) i;
It will produce exact output you want.
This question already has answers here:
Pivot or Transforming JavaScript object
(2 answers)
Closed 3 years ago.
I have the following JSON encoded string that I would like to manipulate using javascript such that the x and y axis values are combined into a two dimensional array names values.
[
{
"key": "0",
"xaxis": "1492041600000",
"yaxis": "512"
},
{
"key": "0",
"xaxis": "1492045200000",
"yaxis": "985"
},
{
"key": "1",
"xaxis": "1492048800000",
"yaxis": "685"
},
{
"key": "1",
"xaxis": "1492052400000",
"yaxis": "935"
}
]
I needs to render as:
[
{
"key": "0",
"values": [
[1492041600000, 512],
[1492045200000, 985]
]
},
{
"key": "1",
"values": [
[1492048800000, 685],
[1492052400000, 935]
]
}
]
Can someone please show me how to perform this data manipulation?
Thank you!
Using map-reduce, you should be able to get the behavior you want.
const results = [{"key":"0","xaxis":"1492041600000","yaxis":"512"},{"key":"0","xaxis":"1492045200000","yaxis":"985"},{"key":"1","xaxis":"1492048800000","yaxis":"685"},{"key":"1","xaxis":"1492052400000","yaxis":"935"}]
const reducedResults = results.reduce((acc, result) => {
if(acc[result.key]) {
acc[result.key].values.push([result.xaxis, result.yaxis]);
} else {
acc[result.key] = { key: result.key, values: [[result.xaxis, result.yaxis]] }
}
return acc;
}, {});
const newResults = Object.values(reducedResults);
console.log(newResults);
This question already has answers here:
Convert array of object to single object in javascript(Angularjs)
(3 answers)
How to convert an array of objects to object with key value pairs
(7 answers)
Reduce array of objects into object [duplicate]
(3 answers)
How to restructure form json data?
(2 answers)
Closed 5 years ago.
I have a json file like this below:
data.json:
[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]
Now, i want to turn this json into javascript object like this format:
JavaScript Object:
{
key0: val0,
key1: val1,
key2: val2,
key3: val3
}
I have tried to find a solution with the functions (like each, map, ...), but i couldn't find a solution. I am not just looking for a solution, but the most efficent one.
Do you have an idea?
reduce is a good option here, as were reducing from an array to an object.
var json = `[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]`;
var obj = JSON.parse(json).reduce((a,v) => { a[v.key] = v.value; return a; }, {});
console.log(obj);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
i have this JSON Object, and i wanna access to this > object.foda.forta.id or name.. in JAVASCRIPT
thanks
note: this json it's created by xml2js.Parser()
{
"object": {
"foda": [
{
"forta": [
{
"id": [
"1"
],
"name": [
"dasdghjg"
]
},
{
"id": [
"2"
],
"name": [
"jj"
]
},
{
"id": [
"3"
],
"name": [
"gjhjg"
]
}
]
}
]
}
}
You cant access by object.foda.forta.id
as foda and fotra are lists, you can access by object.foda[0].forta[0].id
Note - 0 is used for sample only you can use any index (less the size of array)
Your JSON was terribly created, lots of unnecessary Arrays, but you can access it like this:
var obj = {
"object": {
"foda": [
{
"forta": [
{
"id": ["1"],
"name": ["dasdghjg"]
},
{
"id": ["2"],
"name": ["jj"]
},
{
"id": ["3"],
"name": ["gjhjg"]
}
]
}
]
}
};
document.body.innerHTML = "ID: " + obj.object.foda[0].forta[0].id[0] + " - Name: " + obj.object.foda[0].forta[0].name[0];
Take a look at this fiddle!