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}"
*/
Related
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);
}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Hello I have the following JSON result. Its a small snippet from a large JSON file. Theres hundreds of entries.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
I would like a way to loop through this data using javascript to create a new array stored in a variable that pulls just the ids. So the new array would look like this:
data = [1, 2, 3];
If theres 1000 entries, then the array would go from 1-1000.
Thanks!
Very simple just use array.map.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
let idArray = jsonResult.map(element => element["id"]);
console.log(idArray)
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:
Getting the object's property name [duplicate]
(13 answers)
Closed 7 years ago.
Sometimes there are a few values in JSON which are not present as name value pairs but as only names and then their properties and below for example in the JSON below objectOne, ObjectTwo and objectThree. The problem is that there names keep on changing how can I extract them if I don’t know in advance what these names are going to be? But the data structure will be same
{
"Number of objects": 3,
"Devices": {
"objectOne": {
"name": "10",
"name1": "50"
},
"objectTwo": {
"name": "20",
"name1": "30"
},
"objectThree": {
"name": "40",
"name1": "80"
}
}
}
You can try to use Object.keys method.
Sample :
var yourJson = {
"Number of objects": 3,
"Devices": {
"objectOne": {
"name": "10",
"name1": "50"
},
"objectTwo": {
"name": "20",
"name1": "30"
},
"objectThree": {
"name": "40",
"name1": "80"
}
}
}
var keys = Object.keys(yourJson.Devices); // Array with "objectOne", "objectTwo" and "objectThree"
UPDATE :
Then you can access to objectTwo this way :
var objectTwo = yourJson.Devices[keys[1]];
If you need to iterate through all, this is better :
for (var key in keys) {
// key = "objectOne", then "objectTwo", then "objectThree"
var objectN = yourJson.Devices[key]; // the device object
}
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!